我希望能够做到这样的事情:
@Email
public List<String> getEmailAddresses()
{
return this.emailAddresses;
}
Run Code Online (Sandbox Code Playgroud)
换句话说,我希望列表中的每个项目都被验证为电子邮件地址.当然,注释这样的集合是不可接受的.
有没有办法做到这一点?
鉴于:
(def my-vec [{:id 0 :a "foo" :b "bar"} {:id 1 :a "baz" :b "spam"}
{:id 2 :a "qux" :b "fred"}])
Run Code Online (Sandbox Code Playgroud)
如何以惯用方式更新my-vec中的项目以:id=1
获取值:a="baz2"
和:b="spam2"
?
*:我认识到我实际上不会更新my-vec,但实际上返回了一个与my-vec相同的新向量,除了替换值.
(注意:我无法更改我收到的XML的结构.我只能更改我验证它的方式.)
假设我可以像这样得到XML:
<Address Field="Street" Value="123 Main"/>
<Address Field="StreetPartTwo" Value="Unit B"/>
<Address Field="State" Value="CO"/>
<Address Field="Zip" Value="80020"/>
<Address Field="SomeOtherCrazyValue" Value="Foo"/>
Run Code Online (Sandbox Code Playgroud)
我需要创建一个XSD架构,验证"Street","State"和"Zip" 必须存在.但我不在乎"StreetPartTwo"和/或"SomeOtherCrazyValue"是否恰好存在.
如果我知道只有我关心的三个可以被包括(并且每个只包括一次),我可以做这样的事情:
<xs:element name="Address" type="addressType" maxOccurs="unbounded" minOccurs="3"/>
<xs:complexType name="addressType">
<xs:attribute name="Field" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Street"/>
<xs:enumeration value="State"/>
<xs:enumeration value="Zip"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
但是这对我的情况不起作用,因为我也可能会收到那些我不关心的其他Address元素(也有"Field"属性).
任何想法如何我可以确保我关心的东西存在,但也让其他的东西?
TIA!肖恩
说我有一组地图:
(def coll #{{:name "foo"} {:name "bar"}})
Run Code Online (Sandbox Code Playgroud)
我想要一个函数,它将为集合中的每个地图元素添加一个id(唯一的数字很好).即
#{{:id 1 :name "foo"} {:id 2 :name "bar"}}
Run Code Online (Sandbox Code Playgroud)
以下不起作用,但这是我现在的思路.
(defn add-unique-id [coll]
(map assoc :id (iterate inc 0) coll))
Run Code Online (Sandbox Code Playgroud)
提前致谢...
鉴于:
(def my-vec [{:a "foo" :b 10} {:a "bar" :b 13} {:a "baz" :b 7}])
Run Code Online (Sandbox Code Playgroud)
如何迭代每个元素来打印该元素:a和所有的总和:b到那个点?那是:
"foo"10
"bar"23
"baz"30
我正在尝试这样的事情无济于事:
; Does not work!
(map #(prn (:a %2) %1) (iterate #(+ (:b %2) %1) 0)) my-vec)
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为"iterate"lazy-seq不能引用my-vec中的当前元素(据我所知).
TIA!肖恩
举例说明:
(def nest1 {:a {:b {:c "foo"}}})
(def nest2 {:d {:e "bar"}})
Run Code Online (Sandbox Code Playgroud)
如果我想在任意级别上联合这些巢,我可以明确地这样做:
(conj (-> nest1 :a :b) (-> nest2 :d)) ; yields {:e "bar", :c "foo"}
(conj (-> nest1 :a) (-> nest2 :d)) ; yields {:e "bar", :b {:c "foo"}}
Run Code Online (Sandbox Code Playgroud)
但是如果我想创建一个接受nest1和nest2的"深度"作为参数的函数呢?
; Does not work, but shows what I am trying to do
(defn join-nests-by-paths [nest1-path nest2-path]
(conj (-> nest1 nest1-path) (-> nest2 nest2-path))
Run Code Online (Sandbox Code Playgroud)
我可能试着这样称呼它:
; Does not work
(join-nests-by-paths '(:a :b) '(:d))
Run Code Online (Sandbox Code Playgroud)
这不起作用.我不能简单地将每个"路径"作为列表传递给函数(或者我可以,但需要在函数中以不同的方式使用它).
有什么想法吗?TIA ......肖恩