Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing function from other file in clojure

Tags:

clojure

How can i import a function in closure? Suppose i have two files:

test.clj
test2.clj

I want to use the function from test2 in test.

I have tried the following in test, but this is not working:

(namespace user :require test2)

How am a able to import a function in another file?

Basically want to do `from lib import f in python

like image 595
David Avatar asked Mar 11 '26 21:03

David


2 Answers

In file test.clj:

(ns test
  (:require [test2 :as t2]))

(defn myfn [x]
  (t2/somefn x)
  (t2/otherfn x))

In the above example, t2 is an alias for the namespace test2. If instead you prefer to add specified symbols from the namespace use :refer:

(ns test
  (:require [test2 :refer [somefn otherfn]]))

(defn myfn [x]
  (somefn x)
  (otherfn x))

To refer to all public symbols in the namespace, use :refer :all:

(ns test
  (:require [test2 :refer :all]))

(defn myfn [x]
  (somefn x)
  (otherfn x))
like image 109
Steffan Westcott Avatar answered Mar 13 '26 19:03

Steffan Westcott


Your namespace syntax is a bit off. I usually refer to this cheat-sheet when I need a reminder.

I think the following is the syntax you are looking for.

;; In test.clj
(ns test
  (:require [test2 :refer [some-symbol-to-import]]))
like image 42
Erp12 Avatar answered Mar 13 '26 19:03

Erp12



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!