小编noa*_*hlz的帖子

为什么这个以println开头的匿名函数会导致NullPointerException?

我正在学习pmap并编写以下函数:

(pmap #((println "hello from " (-> (Thread/currentThread) .getName)) 
         (+ %1 %2)) 
   [1 1 1] [-1 -1 -1])
Run Code Online (Sandbox Code Playgroud)

运行时,结果是a NullPointerException

(hello from  clojure-agent-send-off-pool-4
hello from  clojure-agent-send-off-pool-3
hello from  clojure-agent-send-off-pool-5
NullPointerException   user/eval55/fn--56 (NO_SOURCE_FILE:11)
Run Code Online (Sandbox Code Playgroud)

为什么会这样?我已经理解并观察到a的主体fn是隐含的do.

clojure

6
推荐指数
1
解决办法
298
查看次数

在Clojure源代码中使用表情符号文字

在启用了UTF-8的控制台上的Linux:

Clojure 1.6.0
user=> (def c \?)
#'user/c
user=> (str c)
"?"
user=> (def c \)

RuntimeException Unsupported character: \  clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Unmatched delimiter: )  clojure.lang.Util.runtimeException (Util.java:221)
Run Code Online (Sandbox Code Playgroud)

我希望能够轻松地拥有一个表情丰富的表情符号Clojure应用程序,但看起来我会查找并输入表情符号代码?或者我错过了一些明显的东西?

unicode clojure emoji

6
推荐指数
1
解决办法
1243
查看次数

Datomic中的参数查询

我正在学习Datomic查询,并对如何进行"参数查询"感到好奇.

这就是我想出的:

(d/q '[:find ?n ?x :where [?n :likes ?x] [(= ?x "pizza")]] 
  [['ethel :likes "sushi"]['fred :likes "pizza"]])

=> #<HashSet [[fred "pizza"]]>
Run Code Online (Sandbox Code Playgroud)

这是它,还是有更简洁/惯用的方式来实现上述目标?

clojure datomic

5
推荐指数
1
解决办法
843
查看次数

习惯性的Clojure方式找到seq中最常见的项目

给定一系列项目,我想找到n个最频繁的项目,按频率降序排列.所以例如我希望这个单元测试通过:

(fact "can find 2 most common items in a sequence"
      (most-frequent-n 2 ["a" "bb" "a" "x" "bb" "ccc" "dddd" "dddd" "bb" "dddd" "bb"]) 
      =>
      '("bb" "dddd"))
Run Code Online (Sandbox Code Playgroud)

我是Clojure的新手,仍然试图掌握标准库.这是我想出的:

(defn- sort-by-val [s]        (sort-by val s))
(defn- first-elements [pairs] (map #(get % 0) pairs))

(defn most-frequent-n [n items]
  "return the most common n items, e.g. 
     (most-frequent-n 2 [:a :b :a :d :x :b :c :d :d :b :d :b])  => 
         => (:d :b)"
  (take n (->
           items               ; [:a :b :a :d …
Run Code Online (Sandbox Code Playgroud)

idiomatic clojure

5
推荐指数
1
解决办法
1752
查看次数

使用 IntelliJ 设置的断点在远程自定义 java 代理的 premain 中不起作用

我已经开始实现一个简单的 Java 代理,它在 JVM 加载类之前执行一些检测。问题是我需要对此代理进行一些调试,但到目前为止我尝试的失败(我尝试远程调试代理但它不起作用)

我使用 IntelliJ 和 Maven(生成 .jar 代理)。

所以我的问题:有什么技巧可以远程调试一个简单的java代理吗?

java debugging intellij-idea agent

5
推荐指数
1
解决办法
8118
查看次数

将列表附加到列表序列

我有一系列列表:

(def s '((1 2) (3 4) (5 6)))
Run Code Online (Sandbox Code Playgroud)

我想将另一个列表附加到该序列的尾部,即

(concat-list s '(7 8))
=> '((1 2) (3 4) (5 6) (7 8))
Run Code Online (Sandbox Code Playgroud)

各种(显然)不起作用的方法:

(cons '((1 2)) '(3 4))
=> (((1 2)) 3 4)

(conj '(3 4) '((1 2)))
=> (((1 2)) 3 4)

(concat '((1 2)) '(3 4))
=> ((1 2) 3 4)

;; close, but wrong order...
(conj '((1 2)) '(3 4))
=> ((3 4) (1 2))

;; Note: vectors work - do I really have to convert …
Run Code Online (Sandbox Code Playgroud)

clojure

5
推荐指数
1
解决办法
4761
查看次数

java项目不会根据源代码顺序编译类引用枚举

我有一种情况,一些Java 1.6源代码不能在Maven中编译,只有在我按照特定顺序指定源代码时才在javac中编译.

pom.xml是赤裸裸的但指定1.6 -source-target1.6:

 <build>
     <plugins>
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>3.1</version>
             <configuration>
                 <source>1.6</source>
                 <target>1.6</target>
             </configuration>
         </plugin>
     </plugins>
 </build>
Run Code Online (Sandbox Code Playgroud)

ComputeOperation.java

package test.domain;

public interface ComputeOperation<T> {

    public T compute(T arg1, T arg2);
}
Run Code Online (Sandbox Code Playgroud)

Computations.java

 package test.domain;

 public enum Computations implements ComputeOperation<Integer> {

     ADD {
         @Override
         public Integer compute(Integer arg1, Integer arg2) {
             return arg1 + arg2;
         }
     },

     SUBTRACT {
         @Override
         public Integer compute(Integer arg1, Integer arg2) {
             return arg1 - arg2;
         }
     }

 } 
Run Code Online (Sandbox Code Playgroud)

App.java

package test.app;    

import …
Run Code Online (Sandbox Code Playgroud)

java compilation maven

5
推荐指数
0
解决办法
109
查看次数

SortedSet映射并不总是保留结果中的元素排序?

鉴于以下Scala 2.9.2代码:

更新了非工作示例

import collection.immutable.SortedSet

case class Bar(s: String)

trait Foo {
  val stuff: SortedSet[String]
  def makeBars(bs: Map[String, String])
    = stuff.map(k => Bar(bs.getOrElse(k, "-"))).toList
}

case class Bazz(rawStuff: List[String]) extends Foo {
  val stuff = SortedSet(rawStuff: _*)
}


// test it out....
val b = Bazz(List("A","B","C"))
b.makeBars(Map("A"->"1","B"->"2","C"->"3"))
// List[Bar] = List(Bar(1), Bar(2), Bar(3))
// Looks good?

// Make a really big list not in order. This is why we pass it to a SortedSet...    
val data =  Stream.continually(util.Random.shuffle(List("A","B","C","D","E","F"))).take(100).toList
val b2 …
Run Code Online (Sandbox Code Playgroud)

scala

5
推荐指数
1
解决办法
1027
查看次数

我可以使用clojure'for'宏来反转字符串吗?

这是我的问题"递归反转Clojure中的序列"的后续内容.

是否可以使用Clojure"for"宏来反转序列?我试图更好地理解这个宏的局限性和用例.

这是我开始的代码:

((defn reverse-with-for [s] 
    (for [c s] c))
Run Code Online (Sandbox Code Playgroud)

可能?

如果是这样,我假设解决方案可能需要for在定义可变var的某个表达式中包装宏,或者for宏的body-expr将以某种方式将序列传递给下一次迭代(类似于map).

clojure

4
推荐指数
2
解决办法
672
查看次数

未指定maxSize时,Google Guava Cache会清除SIZE的条目

我正在使用Google Guava CacheBuilder创建一个Cache实例.我的代码如下所示:

return CacheBuilder.newBuilder()
            .initialCapacity(initialCapacity)
            .expireAfterAccess(expirationInterval, TimeUnit.SECONDS)
            .removalListener(LOGGING_AUTOEVICTION_ONLY_REMOVAL_LISTENER)
            .build();
Run Code Online (Sandbox Code Playgroud)

我的删除监听器,顾名思义:

private static final RemovalListener<MyKey, MyRecord>  LOGGING_AUTOEVICTION_ONLY_REMOVAL_LISTENER
        = new RemovalListener<MyKey, MyRecord>() {
            @Override
            public void onRemoval(RemovalNotification<MyKey, MyRecord objectObjectRemovalNotification) {
                if (objectObjectRemovalNotification.wasEvicted()) {
                    log.debug("Entry evicted from cache: {} - Reason: {}", objectObjectRemovalNotification, objectObjectRemovalNotification.getCause());
                }
            }
        };
Run Code Online (Sandbox Code Playgroud)

最后,我只get使用了我的缓存条目get(K key, Callable<? extends V> valueLoader)

这是问题:使用上面的代码,我看到以下日志输出:

19:08:03.287 DEBUG [MyCache] - Entry evicted from cache: MyKey[123]=MyRecord@43ee148b - Reason: SIZE

当我没有maximumSize()在CacheBuilder调用中使用时,为什么记录会从我的缓存中逐出?我怀疑它与"体重"有关.如果是这样,我需要知道maxWeight()在到期时间之前没有被删除的缓存条目?

注意我知道Google Guava网站上的Caches解释.

java guava

4
推荐指数
1
解决办法
1391
查看次数