With calling a file in Clojure?

Asked

Viewed 75 times

2

Hello, I have already searched in several places, but I did not find the answer I wanted. I have the following function in the archive home.clj

(defn hello [request]
 (let [name (get-in request [:route-params :name])]
     {:status 200
       :body (str "Olá " name ".  Vamos as pergutas!")
       :headers {}}))

And I’d like to call this function "hello" in another file called core clj.. How do I put it in the core.clj header so I can use the functions available in "start.clj"? (would be something with includ<> in C).

2 answers

3


If you want to load a module in LISP you can use require or load.

The difference between them is that the require is specific to load modules written in LISP. Already the load can be used with any file type.

The require function tests whether a module is already present, using a case-by-case comparison, if the module is not present, it prompts you to upload the appropriate file or set of files. The path name argument, if present, is a single path name or a list of path names whose files should be loaded in order, from left to right. If the path name argument is null or is not provided, the system will attempt to determine, in some way dependent on the system, which files to load. This will usually involve some central record of module names and associated file lists.

Syntax:

(require nome-do-módulo [caminho])
  • module name is the name of the module to be loaded.
  • [way] is an optional that informs the file path.

The load function loads the named file into the Lisp environment. It is assumed that a text(character file) can be automatically distinguished from an object(binary) file by some appropriate implementation dependent means, possibly by the file type. The defaults for the file name are obtained from the default variable-pathname-defaults. If the file name (after merging the patterns) does not explicitly specify a type, and the text and object types of the file are available in the file system, the upload should try to select the most appropriate file by some means dependent on the implementation. .

Syntax:

(load filespec [verboso] [print] [if-does-not-exist] [external-format])
  • filespec is a stream or path to the archive.
  • [verbose] optional logical value indicated if the file will be loaded in verbose mode. True for verbose mode and false for silent.
  • [print] optional logical value indicated if the contents of the file will be echoed from standard output(stdio). True to echo and false to not echo.
  • [if-does-not-exist] optional logical value indicated if the file must generate an assertion if it does not exist. True to generate assertion and False not to generate assertion.
  • [External-format--an External] format assigned to the file. Also optional.

For more information on require and load.

  • That’s exactly what I wanted, thank you very much!!

1

First I created an app for example lein new app hello

In src/hello/ created the file inicio.clj:

;;inicio.clj
(ns hello.inicio)

(defn hello [request]
 (let [name (get-in request [:route-params :name])]
     {:status 200
       :body (str "Olá " name ".  Vamos as pergutas!")
       :headers {}}))

And in the core.clj call the job hello of inicio.clj with (:require [hello.inicio :refer [hello]]):

;; core.clj
(ns hello.core
  (:require [hello.inicio :refer [hello]]))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println (hello {:route-params {:name "World"}})))

Result of lein run:

{:status 200, :body Olá World.  Vamos as pergutas!, :headers {}}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.