(use '[clojure.contrib.trace])
(dotrace [str] (reduce str [\a \b]))
Run Code Online (Sandbox Code Playgroud) 我正在为几个星期开始的课程写一些课程材料.我选择Clojure作为学生学习的新语言.我想用一章来重构命令式代码到功能代码.为此,我想提出一系列实用问题,这些问题并不遥远,我可以通过它来展示必要性和功能性解决方案的比较.优选地,问题不像项目Euler那样强调数学,而是普通程序员需要定期处理的问题.谁有这方面的好消息来源?
部分答案:一个这样的来源是http://langref.org/.更想要的!
使用def方法和val函数展平Vs flatMap:
我定义了一个名为toInt的def方法:
def toInt(s: String): Option[Int] = {
try {
Some(Integer.parseInt(s.trim))
} catch {
case e: Exception => None
}
}
Run Code Online (Sandbox Code Playgroud)
这个方法适用于flatten和flatMap,如下所示:
//using toInt method
val x = 1.to(5).toList
val y = List("a")
val z = x ++ y
val q = z.map(_.toString)
//using map and flatten
println(q.map(toInt).flatten)
//using flatMap
println(q.flatMap(toInt))
Run Code Online (Sandbox Code Playgroud)
现在我在函数"tooInt"中使用val定义了相同的toInt功能(如在def方法中):
val tooInt: String => Option[Int] = s => {
try {
Some(Integer.parseInt(s.trim))
} catch {
case c: Exception => None
}
}
Run Code Online (Sandbox Code Playgroud)
这正常工作与扁平化,但不如下图所示与flatMap: …
在Emacs中使用dired模式的最有用的例子是什么?有什么好玩的吗?请回答一个例子.
我在我正在开发的本地wep-app中使用名为'mydb'的mongodb数据库,并希望通过Dropbox同步db文件,因此我将在所有开发机器上使用它.
每当我在数据库中插入一些新数据时,文件'mydb.0'和'mydb.ns'似乎都没有改变.因此Dropbox不会同步任何内容.这里有什么想法?
我知道这可能听起来像一个可怕的想法,但我是唯一的数据库用户,我从不在一台机器上运行数据库.此外,我不与其他任何人共享文件.只是要注意我可以在我离开的地方继续使用另一台机器.
当我发表评论时,为什么子组件中的计数器更新正常
(om/update-state! owner :clicked not)
Run Code Online (Sandbox Code Playgroud)
而不是当我在下面的代码中的父组件中取消注释它?单击按钮更新计数器.
我想要实现的是一个pub/sub机制,因此组件可以以分离的方式交换消息.
您可以通过以下方式创建一个新项目来复制它:
lein new mies-om om-channel-test
Run Code Online (Sandbox Code Playgroud)
然后用下面的代码替换core.cljs并运行
lein cljsbuild auto
Run Code Online (Sandbox Code Playgroud)
在现代浏览器中访问index.html页面(例如最新的Chrome).
代码:
(ns om-channel-test.core
(:require-macros [cljs.core.async.macros :refer (go)])
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[cljs.core.async :refer [chan pub <! sub >! timeout put!]]))
(enable-console-print!)
(def app-state (atom {:text "Hello world!"}))
(def event-ch (chan))
(def event-pub
(pub event-ch #(:topic %)))
(defn child [cursor owner]
(reify
om/IInitState
(init-state [_]
{:counter 0})
om/IWillMount
(will-mount [_]
(go (loop [] (<! (om/get-state owner :subscriber)) …Run Code Online (Sandbox Code Playgroud) 剧透警报,这是代码日第6天的第一部分.
我试图在Clojure和Scala中解决这个问题.Scala程序运行良好,并在我的Macbook Air上几秒钟内完成.然而,Clojure版本需要更长的时间.这里有什么问题?
(ns adventofcode.day6
(:require [clojure.java.io :as io]
[clojure.string :as string]))
(set! *warn-on-reflection* true)
(def ^:const grid-size 1000)
(defn make-grid []
(let [grid (make-array Long/TYPE grid-size grid-size)]
(doseq [i (range grid-size)
j (range grid-size)]
(aset grid i j -1))
grid))
(defn parse-line [l]
(let [[command left-top right-bottom]
(-> l
(string/replace "turn off" "turn-off")
(string/replace "turn on" "turn-on")
(string/replace "through " "")
(string/split #" "))
parse-coordinates (fn [s]
(let [parts (string/split s #",")]
(map #(Integer/parseInt %) parts)))
[left …Run Code Online (Sandbox Code Playgroud) 我想将JSON字符串插入具有jsonb字段的Postgres表中,并希望插入查询返回JSON的一部分。例如,我想在下面的示例中返回ID。问号上出现了什么?
insert into mytable (myjson)
values ('{"id":123}') returning ???
Run Code Online (Sandbox Code Playgroud) 是否可以表达一个upsert查询,如果插入的数据与数据库中已有的数据相比没有任何变化,那么什么都不会发生?
目前我有:
insert into feeds (link, title, category)
values (...)
on conflict (link)
do update set (title, category, _updated)
= (..., now()::timestamp)
Run Code Online (Sandbox Code Playgroud)