Clojure调用一系列函数并存储其返回值 [英] Clojure call series of functions and store their return values

查看:79
本文介绍了Clojure调用一系列函数并存储其返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个原子模式,并在 clj 文件的底部具有以下内容,该文件定义并处理模式和初始数据。每次调用下面被调用的函数 d / transact

I'm building a datomic schema and have the following at the foot of my clj file which defines and transacts schema and initial data. The functions being called below each call d/transact.

(defn recreate-database []
  "To recreate db after running delete-database in bin/repl"
  (pt1-transact-schema)
  (pt1-transact-data)
  (pt2-transact-schema)
  (pt2-transact-data)
  (pt3-transact-schema)
  (pt3-transact-data))

默认情况下,我们只看到最后一个表单的返回值,但我想查看或保存,六个函数调用中每个函数的结果。

By default we only see the return value of the last form, but I'd like to see, or save, the result of each of the six function calls.

想知道这样做是一种不错的方法。

Wondering what a nice way to do this is.

想到了类似(map(comp println eval)[functions])的想法,但这是不对的。 / p>

Thought of something like (map (comp println eval) [functions]), but that's not right.

推荐答案

还有一个很好的功能组合函数,称为 juxt

there is also a nice functional composition function called juxt:

user> ((juxt + - * /) 1 2)
;;=> [3 -1 2 1/2]

user> ((juxt (constantly 1) (constantly 2) (constantly 3)))
;;=> [1 2 3]

,或者您的情况:

(def recreate-database (juxt pt1-transact-schema
                             pt1-transact-data
                             pt2-transact-schema
                             pt2-transact-data
                             pt3-transact-schema
                             pt3-transact-data))

这篇关于Clojure调用一系列函数并存储其返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆