Skip to content

Commit fad1492

Browse files
authored
Reorganize basilisp.core into more logical groups (#274)
1 parent 31b1420 commit fad1492

File tree

1 file changed

+141
-112
lines changed

1 file changed

+141
-112
lines changed

src/basilisp/core/__init__.lpy

Lines changed: 141 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,47 @@
264264
(recur (rest s))
265265
(first s)))
266266

267+
(defn str
268+
"Create a string representation of o."
269+
([] "")
270+
([o] (builtins/str o))
271+
([o & args]
272+
(let [coerce (fn [in out]
273+
(if (seq (rest in))
274+
(recur (rest in)
275+
(conj out (builtins/str (first in))))
276+
(conj out (builtins/str (first in)))))
277+
strs (coerce (conj args o) [])]
278+
(.join "" strs))))
279+
280+
(defn symbol
281+
"Create a new symbol with name and optional namespace ns."
282+
([name]
283+
(basilisp.lang.symbol/symbol name))
284+
([ns name]
285+
(basilisp.lang.symbol/symbol name ns)))
286+
287+
(defn keyword
288+
"Create a new keyword with name and optional namespace ns. Keywords
289+
will have the colon prefix added automatically, so it should not be
290+
provided."
291+
([name]
292+
(basilisp.lang.keyword/keyword name))
293+
([ns name]
294+
(basilisp.lang.keyword/keyword name ns)))
295+
296+
(defn name
297+
"Return the name of a string, symbol, or keyword."
298+
[v]
299+
(if (string? v)
300+
v
301+
(.-name v)))
302+
303+
(defn namespace
304+
"Return the namespace of a symbol or keyword, or nil if no namespace."
305+
[v]
306+
(.-ns v))
307+
267308
(def
268309
^{:macro true
269310
:doc "Define a new macro like defn. Macro functions are available to the
@@ -304,6 +345,10 @@
304345
`(defn ~fname
305346
~@body))))
306347

348+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
349+
;; Logical Comparisons & Macros ;;
350+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
351+
307352
(defmacro if-not
308353
"Evaluate cond and if it is true, return false-cond. Otherwise return
309354
true-cond."
@@ -350,6 +395,20 @@
350395
(or ~@(rest args)))
351396
(first args))))
352397

398+
(defmacro cond
399+
"Given groups of test/expression pairs, evaluate each test and, if
400+
true, return the expression. Otherwise, continue through until reaching
401+
the final expression."
402+
[& clauses]
403+
(when (seq clauses)
404+
(list 'if (first clauses)
405+
(if (next clauses)
406+
(second clauses)
407+
(throw
408+
(ex-info "cond requires an even number of forms"
409+
{:first (first clauses)})))
410+
(cons 'basilisp.core/cond (nthrest clauses 2)))))
411+
353412
(defn not
354413
"Return the logical negation of expr."
355414
[expr]
@@ -435,60 +494,9 @@
435494
false)
436495
(operator/le x (first args)))))
437496

438-
(defn str
439-
"Create a string representation of o."
440-
([] "")
441-
([o] (builtins/str o))
442-
([o & args]
443-
(let [coerce (fn [in out]
444-
(if (seq (rest in))
445-
(recur (rest in)
446-
(conj out (builtins/str (first in))))
447-
(conj out (builtins/str (first in)))))
448-
strs (coerce (conj args o) [])]
449-
(.join "" strs))))
450-
451-
(defmacro cond
452-
"Given groups of test/expression pairs, evaluate each test and, if
453-
true, return the expression. Otherwise, continue through until reaching
454-
the final expression."
455-
[& clauses]
456-
(when (seq clauses)
457-
(list 'if (first clauses)
458-
(if (next clauses)
459-
(second clauses)
460-
(throw
461-
(ex-info "cond requires an even number of forms"
462-
{:first (first clauses)})))
463-
(cons 'basilisp.core/cond (nthrest clauses 2)))))
464-
465-
(defn symbol
466-
"Create a new symbol with name and optional namespace ns."
467-
([name]
468-
(basilisp.lang.symbol/symbol name))
469-
([ns name]
470-
(basilisp.lang.symbol/symbol name ns)))
471-
472-
(defn keyword
473-
"Create a new keyword with name and optional namespace ns. Keywords
474-
will have the colon prefix added automatically, so it should not be
475-
provided."
476-
([name]
477-
(basilisp.lang.keyword/keyword name))
478-
([ns name]
479-
(basilisp.lang.keyword/keyword name ns)))
480-
481-
(defn name
482-
"Return the name of a string, symbol, or keyword."
483-
[v]
484-
(if (string? v)
485-
v
486-
(.-name v)))
487-
488-
(defn namespace
489-
"Return the namespace of a symbol or keyword, or nil if no namespace."
490-
[v]
491-
(.-ns v))
497+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
498+
;; State Management Functions ;;
499+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
492500

493501
(defn deref
494502
"Dereference a delay or atom and returns its contents."
@@ -538,33 +546,9 @@
538546
(list 'basilisp.lang.delay/Delay
539547
(concat '(fn* []) body)))
540548

541-
(defmacro lazy-seq
542-
"Takes a body of expressions which will produce a seq or nil. When
543-
seq is first called on the resulting lazy-seq, the sequence will be
544-
realized."
545-
[& body]
546-
(list 'basilisp.lang.seq/LazySeq
547-
(concat '(fn* []) body)))
548-
549-
(defn pos?
550-
"Return true if x is positive."
551-
[x]
552-
(if (operator/gt x 0) true false))
553-
554-
(defn non-neg?
555-
"Return true if x is not negative."
556-
[x]
557-
(if (operator/ge x 0) true false))
558-
559-
(defn zero?
560-
"Return true if x is 0."
561-
[x]
562-
(= 0 x))
563-
564-
(defn neg?
565-
"Return true if x is negative."
566-
[x]
567-
(if (operator/lt x 0) true false))
549+
;;;;;;;;;;;;;;;;;;;;;;;;;;
550+
;; Arithmetic Functions ;;
551+
;;;;;;;;;;;;;;;;;;;;;;;;;;
568552

569553
(defn +
570554
"Sum the arguments together. If no arguments given, returns 0."
@@ -627,6 +611,26 @@
627611
[x]
628612
(- x 1))
629613

614+
(defn pos?
615+
"Return true if x is positive."
616+
[x]
617+
(if (operator/gt x 0) true false))
618+
619+
(defn non-neg?
620+
"Return true if x is not negative."
621+
[x]
622+
(if (operator/ge x 0) true false))
623+
624+
(defn zero?
625+
"Return true if x is 0."
626+
[x]
627+
(= 0 x))
628+
629+
(defn neg?
630+
"Return true if x is negative."
631+
[x]
632+
(if (operator/lt x 0) true false))
633+
630634
(defn even?
631635
"Return true if x is even."
632636
[x]
@@ -654,6 +658,10 @@
654658
([cmp coll]
655659
(basilisp.lang.runtime/sort coll cmp)))
656660

661+
;;;;;;;;;;;;;;;;;;;;;;;;;;;
662+
;; Associative Functions ;;
663+
;;;;;;;;;;;;;;;;;;;;;;;;;;;
664+
657665
(defn contains?
658666
"Return true if coll contains k. For vectors, k is an index. For maps, k is
659667
a key. For sets, k is a value in the set."
@@ -690,38 +698,17 @@
690698
[entry]
691699
(.-value entry))
692700

693-
(defmacro new
694-
"Create a new instance of class with args.
695-
696-
New objects may be created as any of:
697-
(new builtins/str *args)
698-
(new builtins.str *args)
699-
(new builtins.str. *args)
700-
701-
This is compatibility syntax for Clojure, since Python (and therefore
702-
Basilisp) do not require the new keyword for object instantiation."
703-
[class & args]
704-
(cond
705-
(not (symbol? class))
706-
(throw
707-
(ex-info "Expected a class name as a symbol"
708-
{:class-name class}))
709-
710-
(namespace class)
711-
(let [n (name class)
712-
ns (namespace class)
713-
s (symbol (str ns "."
714-
(if (.endswith n ".")
715-
n
716-
(str n "."))))]
717-
`(~s ~@args))
701+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
702+
;; Higher Order Functions ;;
703+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
718704

719-
:else
720-
(let [n (name class)
721-
s (symbol (if (.endswith n ".")
722-
n
723-
(str n ".")))]
724-
`(~s ~@args))))
705+
(defmacro lazy-seq
706+
"Takes a body of expressions which will produce a seq or nil. When
707+
seq is first called on the resulting lazy-seq, the sequence will be
708+
realized."
709+
[& body]
710+
(list 'basilisp.lang.seq/LazySeq
711+
(concat '(fn* []) body)))
725712

726713
(defn range
727714
"Return a range of integers from start. If end is specified, the
@@ -955,6 +942,48 @@
955942
{}
956943
maps)))
957944

945+
;;;;;;;;;;;;;;;;;;;;
946+
;; Utility Macros ;;
947+
;;;;;;;;;;;;;;;;;;;;
948+
949+
(defmacro apply-method
950+
"Apply arguments to a method call. Equivalent to (apply (.-method o) args)."
951+
[o method & args]
952+
`(apply (.- ~o ~method) ~@args))
953+
954+
(defmacro new
955+
"Create a new instance of class with args.
956+
957+
New objects may be created as any of:
958+
(new builtins/str *args)
959+
(new builtins.str *args)
960+
(new builtins.str. *args)
961+
962+
This is compatibility syntax for Clojure, since Python (and therefore
963+
Basilisp) do not require the new keyword for object instantiation."
964+
[class & args]
965+
(cond
966+
(not (symbol? class))
967+
(throw
968+
(ex-info "Expected a class name as a symbol"
969+
{:class-name class}))
970+
971+
(namespace class)
972+
(let [n (name class)
973+
ns (namespace class)
974+
s (symbol (str ns "."
975+
(if (.endswith n ".")
976+
n
977+
(str n "."))))]
978+
`(~s ~@args))
979+
980+
:else
981+
(let [n (name class)
982+
s (symbol (if (.endswith n ".")
983+
n
984+
(str n ".")))]
985+
`(~s ~@args))))
986+
958987
(defmacro binding
959988
"Establish thread-local bindings for the vars given. The bindings are guaranteed
960989
to clear once execution passes outside the scope of this block."

0 commit comments

Comments
 (0)