在阅读了介绍传感器的Clojure(http://blog.podsnap.com/ducers2.html)上的这篇文章之后,我对传感器的含义感到困惑.是否部分应用于mapHaskell,如map (+1)传感器?起初我认为这是使用部分应用程序的Clojure方式,但随后本文继续在Haskell中使用显式类型实现它.它在Haskell中有什么用?
我需要有关从UTF-8文件中删除BOM的方法的建议,并创建其余xml文件的副本.
您好我正在学习clojure,我想了解当我打字时发生了什么
(map println '(1 2 3 4))
Run Code Online (Sandbox Code Playgroud)
我期待类似的东西
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
但我明白了
(1
2
nil 3
nil 4
nil nil)
Run Code Online (Sandbox Code Playgroud)
这只是我编写的一个例子.我只是想了解发生了什么.也许与懒惰有关?
我在基于java的系统中工作,我需要为可视显示中的某些元素设置id.一类元素是字符串,所以我决定使用String.hashCode()方法来获取这些元素的唯一标识符.
然而,我遇到的问题是,如果id是负数并且String.hashCode经常返回负值,我在borks工作的系统.一个快速的解决方案是在hashcode调用周围使用Math.abs()来保证肯定的结果.我对这种方法感到疑惑的是,两个不同元素具有相同哈希码的可能性是多少?
例如,如果一个字符串返回的哈希码为-10,另一个字符串返回的哈希码为10,则会发生错误.在我的系统中,我们讨论的是通常不超过30个元素的对象集合,所以我不认为这确实是一个问题,但我很好奇数学所说的.
在开发期间,我想为使用schema.core/defn定义的所有函数启用验证,而不是必须使用它们进行注释:^:always-validate.这个库有可能吗?
这样的东西不起作用,可能是因为元数据只在编译时添加:
(def dev false)
;; example of schema validation
(sm/defn ^{:always-validate dev}
add :- s/Num
[a :- s/Num b :- s/Num]
(+ a b))
Run Code Online (Sandbox Code Playgroud) 我之前没有使用过很多静态方法,但最近我倾向于使用更多静态方法.例如,如果我想在类中设置一个布尔标志,或者在一个类中设置一个布尔标志而不需要通过类传递实际对象.
例如:
public class MainLoop
{
private static volatile boolean finished = false;
public void run()
{
while ( !finished )
{
// Do stuff
}
}
// Can be used to shut the application down from other classes, without having the actual object
public static void endApplication()
{
MainLoop.finished = true;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我应该避免的吗?传递一个对象以便使用对象方法更好吗?布尔值finished现在是否计为全局,或者它是否同样安全?
require来自clojure的包装很容易:
user=> (require 'clojure.core)
nil
Run Code Online (Sandbox Code Playgroud)
但是如何.jar在Clojure中需要一个文件?
(我正在努力学习lein.Leiningen对大项目而不是小脚本很有用.所以我想弄清楚jar在我的Clojure脚本和REPL中使用包的最佳方法是什么.)
环境:Clojure 1.4
我试图从函数向量中动态提取函数元数据.
(defn #^{:tau-or-pi: :pi} funca "doc for func a" {:ans 42} [x] (* x x))
(defn #^{:tau-or-pi: :tau} funcb "doc for func b" {:ans 43} [x] (* x x x))
(def funcs [funca funcb])
Run Code Online (Sandbox Code Playgroud)
现在,检索REPL中的元数据(有点)是直截了当的:
user=>(:tau-or-pi (meta #'funca))
:pi
user=>(:ans (meta #'funca))
42
user=>(:tau-or-pi (meta #'funcb))
:tau
user=>(:ans (meta #'funcb))
43
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试做一个地图,让:ans,:tau-or-pi或基本:name从元数据,我得到异常:
user=>(map #(meta #'%) funcs)
CompilerException java.lang.RuntimeException: Unable to resolve var: p1__1637# in this context, compiling:(NO_SOURCE_PATH:1)
Run Code Online (Sandbox Code Playgroud)
在做了一些搜索后,我从2009年的帖子中得到了以下想法(https://groups.google.com/forum/?fromgroups=#!topic/clojure/VyDM0YAzF4o):
user=>(map #(meta …Run Code Online (Sandbox Code Playgroud) 大家好:clojure"atom"的文档说明 -
"Changes to atoms are always free of race conditions."
Run Code Online (Sandbox Code Playgroud)
然而,竞争条件不仅在变化方面定义,而且在不同线程中的并行逻辑操作的上下文中定义.
我想知道 - "原子的变化总是没有竞争条件"的保证有什么意义?在java中,我们有原子基元,它支持某些特定的线程安全操作(例如,AtomicInteger支持"getAndIncrement"操作).但是Clojure原子是类型无关的,例如,我们可以调用:
(atom "Hi im a string") Or
(atom (.getClass Object))
Run Code Online (Sandbox Code Playgroud)
原子方法的灵活性意味着,引擎盖下的Clojure并非"巧妙地"为原子提供特定类型的原子/线程安全操作.
因此,我会问 - 原子方法究竟对我们的对象做了什么(即它是简单地同步整个对象?)
为了使类不可变,我能做的是:
1)使类最终
2)不提供setter
3)将所有变量标记为final
但是如果我的类有另一个类的其他对象,那么somone可以改变该对象的值
class MyClass{
final int a;
final OtherClass other
MyClass(int a ,OtherClass other){
this.a = a;
this.other = other;
}
int getA(){
return a;
}
OtherClass getOther(){
return other;
}
public static void main(String ags[]){
MyClass m = new Myclass(1,new OtherClass);
Other o = m.getOther();
o.setSomething(xyz) ; //This is the problem ,How to prevent this?
}
}
Run Code Online (Sandbox Code Playgroud)