小编Vin*_*inn的帖子

在 Clojure 中使用 clj-http 发布请求 - 正文不被接受?

通过我的发布请求,我希望发布的 CRM API 文档也要求我发布 JSON 文件。

JSON 文件是一个多级文件,在 clojure 中被视为持久数组映射。

我要发布的代码是:

(def contacts (http/post "https://api.close.com/api/v1/data/search" 
           {:basic-auth [api ""]
            :body closeFilter 
            })) 
Run Code Online (Sandbox Code Playgroud)

CloseFilter 代表我希望发布的多级 JSON。

但是,我收到以下错误:

class clojure.lang.PersistentArrayMap cannot be cast to class [B (clojure.lang.PersistentArrayMap is in unnamed module of loader 'app'; [B is in module java.base of loader 'bootstrap')

我在这里犯了什么错误?

更新

我正在用 Javascript 重新创建一个程序。发布相同的文件效果很好。

更新 2 - MRE

我仍然在努力解决这个问题,所以这是我的代码示例。

我的代码首先需要我需要的包:

(ns schedule-emails.core
  (:require [clj-http.client :as http]
            [clojure.data.json :as json]
            [cheshire.core :refer :all]))
Run Code Online (Sandbox Code Playgroud)

然后,我将文件系统中的本地 JSON 文件解析到应用程序中。JSON。这将返回带有嵌入向量的地图的地图。

(def closeFilter
  (json/read-str
   (slurp "URL …
Run Code Online (Sandbox Code Playgroud)

post clojure clj-http

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

了解 Common Lisp 中的泛型函数?

在这个答案中,用户给出了一个非常清晰的示例,说明类和方法如何协同工作。

我将在这里重印该示例:


(defclass human () ())
(defclass dog () ())

(defmethod greet ((thing human))
  (print "Hi human!"))

(defmethod greet ((thing dog))
  (print "Wolf-wolf dog!"))

(defparameter Anna (make-instance 'human))
(defparameter Rex (make-instance 'dog))

(greet Anna) ;; => "Hi human"
(greet Rex)  ;; => "Wolf-wolf dog!"

Run Code Online (Sandbox Code Playgroud)

我的问题是,使用相同的示例:

  1. 创建通用函数会增加什么价值?
  2. 为什么泛型函数有用?它们是否像其他面向对象语言中提供结构的实例一样?

似乎通用函数是在后台隐式创建的(不是 100% 确定)。我注意到,当我使用这个示例时,如果我创建一个具有与该方法的第一个实例不同的参数结构的方法,我会得到一个generic function error.

methods function common-lisp clos generic-function

3
推荐指数
1
解决办法
565
查看次数

提取需要经常重新读取的 CSV 数据?

大多数时候,当我读取 CSV 数据时,我使用局部变量:


(defun do-something ()
...
  (let ((thing (read-csv #P"~/Desktop/file.csv")))

   .... do stuff with thing))

Run Code Online (Sandbox Code Playgroud)

这有效。然而,我的一些功能变得相当庞大和混乱。所以我尝试将读取数据的函数放在一个特殊变量中:


(defparameter *file* (read-csv #P"~/Desktop/file.csv"))

Run Code Online (Sandbox Code Playgroud)

但是当我使用该*file*变量时,它不会使用新的 file.csv 进行更新。

有没有办法强制刷新或者也许是实时提取 CSV 数据的更好方法?

variables sbcl common-lisp

3
推荐指数
1
解决办法
57
查看次数

何时以及为何在 CL 中使用哈希表而不是 a-list?

我一直使用 a-lists。

您何时以及为何(或应该)使用哈希表?

我不愿意使用它们是因为,与其他数据结构不同,CL 中的哈希表不是可见的列表。老实说,考虑到几乎所有东西都是列表,我觉得很奇怪。

也许我因缺乏经验而错过了一些东西?

hashtable sbcl common-lisp data-structures

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

Common Lisp 中的递归与应用?

当我出于学习目的阅读代码时,我经常看到这样的函数:

(defun min-list (lst)
  (cond ((endp (rest lst)) (first lst))
    (t
     (min (first lst) (min-list (rest lst))))))

Run Code Online (Sandbox Code Playgroud)

如果我在我的项目中编写这个函数,我会用apply.

(defun min-list2 (lst)
  (cond ((endp (rest lst)) (first lst))
    (t
     (apply #'min lst))))))
Run Code Online (Sandbox Code Playgroud)

当我用 测试这两个变体时time,它们似乎执行相同的操作。

有关系吗?

recursion common-lisp apply

2
推荐指数
1
解决办法
66
查看次数

理解 CLOS:后续方法和主要方法

在 hunchentoot 源代码中,有一个:after名为initalize-instance 的def 方法。

这个特定的示例是整个项目中调用的少数:after方法之一。initalize-instance

我使用 CLOS 的经验告诉我, 需要一个主要方法:before:after并且:around可以使用方法。

但在 hunchentoot 包中,我看不到他们如何创建主要方法以使这些:after方法起作用。

我缺少什么?

sbcl common-lisp clos generic-function method-combination

2
推荐指数
1
解决办法
120
查看次数

函数参数在列表中无法识别?

我正在使用 dexator 来执行发布请求。我正在更新联系人数据库。

这是我的代码:

(defun upload-post (firstname email &optional first-line)
  
  (dex:post NEW-LEAD-URL
        :headers '((:content-type . "application/json"))
        :content '(("email" . 'email)
               ("first_name" . firstname)
               ("custom_attributes" . '(("first_line" . first-line))))))


Run Code Online (Sandbox Code Playgroud)

firstname, email and first-line我收到已定义但未使用的错误。

请注意,我测试了引用email. 那行不通。

为什么参数被忽略?

function common-lisp

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

从 Common Lisp 中的函数返回特定值?

我用clsql写了一个函数。它所做的只是读取整个表。首先它打开连接,读取,然后关闭。

(defun select()
  (clsql:connect "new.db" :database-type :sqlite3)

  (clsql:print-query
   "select * from contacts"
   :titles '("id" "firstname" "email" "company" "firstline" "status"))
  (clsql:disconnect :database "new.db"))

Run Code Online (Sandbox Code Playgroud)

有了disconnect最后一个表达式,我得到T返回值。

我想得到的值clsql:print-query返回的值。但是,断开连接应该最后进行,因为我需要确保连接关闭。

我尝试block过并且return-with,但没有运气。

接近返回值的最佳方法是什么

function sbcl common-lisp

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

为什么可执行文件的行为与 repl 的行为不同?

我的设置

我用 emacs + slime 开发 common lisp。我的机器是 mac pro M1。而且,我使用 kitty 终端模拟器。

情况

当我在 repl 中运行我的应用程序(参见末尾的代码)时,它工作正常。

当我创建并运行可执行文件时,就好像程序的顺序不同了。

例如,我有 5 个问题需要用户输入......


(defpackage custom-sender
  ;; import the exactly the symbols I need from a package
  (:import-from :cl-json :encode-json-to-string)
  (:use :cl)
  (:export :main))

(in-package custom-sender)

(defun main()
  (asking-questions))


(defun asking-questions ()
  (let ((firstname (prompt-read "What is the firstname of the contact?"))
        (email (prompt-read "What is their email?"))
        (job (prompt-read "What is the job?"))
        (first-line (prompt-read "what is the first line of their …
Run Code Online (Sandbox Code Playgroud)

executable sbcl common-lisp

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

with-open 宏在 clojure 中的幕后作用是什么?

从视觉上看,with-open看起来类似于let。我知道 with-open 是有不同的目的,但我找不到关于正在with-open做什么的明确答案。并且,with-open 中的第一个参数是什么?

文档是这样说的:

“bindings => [name init ...] 评估 try 表达式中的主体,其中名称绑定到 inits 的值,以及以相反顺序对每个名称调用 (.close name) 的 finally 子句。”

我不明白。如果有人解释 with-open 的实际用途,我将不胜感激?

clojure

0
推荐指数
1
解决办法
84
查看次数