368 lines
9.7 KiB
Clojure
368 lines
9.7 KiB
Clojure
(ns clje.core
|
|
"CljElixir core — stubs for clj-kondo/clojure-lsp.
|
|
|
|
These are builtin functions and special forms handled directly by the
|
|
CljElixir transformer. Standard Clojure forms (defn, let, fn, if, etc.)
|
|
are mapped via :lint-as in .clj-kondo/config.edn."
|
|
(:refer-clojure :exclude [case send pr pr-str prn print-str
|
|
vec vector subvec vector?
|
|
str println cons list
|
|
inc dec
|
|
map filter concat take drop
|
|
sort sort-by group-by frequencies distinct
|
|
mapcat partition
|
|
keys vals select-keys merge into
|
|
get assoc dissoc update get-in assoc-in update-in
|
|
count first rest seq
|
|
conj nth peek pop
|
|
reduce reduce-kv
|
|
contains? empty? nil?
|
|
not= with-open throw]))
|
|
|
|
;; ===== Special Forms (hooks handle these) =====
|
|
(defmacro receive
|
|
"BEAM message receive with pattern matching.
|
|
(receive
|
|
[:hello sender] (send sender :hi)
|
|
[:quit] (System/halt 0)
|
|
:after 5000 (println \"timeout\"))"
|
|
[& clauses])
|
|
(defmacro defmodule
|
|
"Defines an Elixir module.
|
|
(defmodule MyModule
|
|
(defn my-func [x] (* x 2)))"
|
|
[name & body])
|
|
(defmacro case
|
|
"Pattern matching (not constant matching like Clojure).
|
|
(case val
|
|
[a b] (+ a b)
|
|
{:key v} v
|
|
_ :default)"
|
|
[expr & clauses])
|
|
(defmacro with
|
|
"Monadic binding — chains pattern matches, short-circuits on mismatch.
|
|
(with [{:ok val1} (step1)
|
|
{:ok val2} (step2 val1)]
|
|
(+ val1 val2))"
|
|
[bindings & body])
|
|
(defmacro ns
|
|
"Declares the module namespace (primary module declaration).
|
|
(ns MyApp.Router
|
|
(require [Logger])
|
|
(use [CljElixir.Core]))"
|
|
[name & body])
|
|
|
|
;; ===== BEAM Concurrency =====
|
|
(defn spawn
|
|
"Spawns a new BEAM process. Returns PID.
|
|
(spawn (fn [] (IO/puts \"hello from new process\")))"
|
|
[f])
|
|
(defn send
|
|
"Sends a message to a process. Returns the message.
|
|
(send pid {:hello \"world\"})"
|
|
[pid msg])
|
|
(defn monitor
|
|
"Monitors a process. Returns a reference.
|
|
(monitor pid)
|
|
(monitor :process pid)"
|
|
([pid]) ([type pid]))
|
|
(defn link
|
|
"Creates a bidirectional link between processes.
|
|
(link pid)"
|
|
[pid])
|
|
(defn unlink
|
|
"Removes a link between processes.
|
|
(unlink pid)"
|
|
[pid])
|
|
(defn alive?
|
|
"Returns true if the process is alive.
|
|
(alive? pid) ;=> true"
|
|
[pid])
|
|
|
|
;; ===== Arithmetic =====
|
|
(defn inc
|
|
"Increments by 1.
|
|
(inc 5) ;=> 6"
|
|
[x])
|
|
(defn dec
|
|
"Decrements by 1.
|
|
(dec 5) ;=> 4"
|
|
[x])
|
|
|
|
;; ===== String & Output =====
|
|
(defn str
|
|
"Concatenates arguments into a string.
|
|
(str \"hello\" \" \" \"world\") ;=> \"hello world\""
|
|
[& args])
|
|
(defn println
|
|
"Prints arguments followed by newline.
|
|
(println \"hello\")"
|
|
[& args])
|
|
(defn pr-str
|
|
"Returns a string representation (EDN-like).
|
|
(pr-str {:a 1}) ;=> \"{:a 1}\""
|
|
[& args])
|
|
(defn prn
|
|
"Prints EDN representation followed by newline."
|
|
[& args])
|
|
(defn pr
|
|
"Prints EDN representation (no newline)."
|
|
[& args])
|
|
(defn print-str
|
|
"Returns a human-readable string."
|
|
[& args])
|
|
|
|
;; ===== BEAM Types & Interop =====
|
|
(defn tuple
|
|
"Creates a BEAM tuple from arguments.
|
|
(tuple :ok \"value\") ;=> #el[:ok \"value\"]"
|
|
[& args])
|
|
(defn clojurify
|
|
"Converts Elixir types to Clojure equivalents (keyword lists → maps, etc.).
|
|
(clojurify elixir-val)"
|
|
[val])
|
|
(defn elixirify
|
|
"Converts Clojure types to Elixir equivalents (maps → keyword lists, etc.).
|
|
(elixirify clj-val)"
|
|
[val])
|
|
(defn hd
|
|
"Returns the head (first element) of a list.
|
|
(hd [1 2 3]) ;=> 1"
|
|
[coll])
|
|
(defn tl
|
|
"Returns the tail (rest) of a list.
|
|
(tl [1 2 3]) ;=> [2 3]"
|
|
[coll])
|
|
(defn cons
|
|
"Prepends an element to a list.
|
|
(cons 1 [2 3]) ;=> [1 2 3]"
|
|
[head tail])
|
|
(defn list
|
|
"Creates a list from arguments.
|
|
(list 1 2 3) ;=> [1 2 3]"
|
|
[& args])
|
|
|
|
;; ===== BEAM Type Guards =====
|
|
(defn is-binary
|
|
"Returns true if `x` is a binary (string). Allowed in guards.
|
|
(is-binary \"hello\") ;=> true"
|
|
[x])
|
|
(defn is-integer
|
|
"Returns true if `x` is an integer. Allowed in guards."
|
|
[x])
|
|
(defn is-float
|
|
"Returns true if `x` is a float. Allowed in guards."
|
|
[x])
|
|
(defn is-number
|
|
"Returns true if `x` is a number. Allowed in guards."
|
|
[x])
|
|
(defn is-atom
|
|
"Returns true if `x` is an atom. Allowed in guards."
|
|
[x])
|
|
(defn is-list
|
|
"Returns true if `x` is a list. Allowed in guards."
|
|
[x])
|
|
(defn is-map
|
|
"Returns true if `x` is a map. Allowed in guards."
|
|
[x])
|
|
(defn is-tuple
|
|
"Returns true if `x` is a tuple. Allowed in guards."
|
|
[x])
|
|
(defn is-pid
|
|
"Returns true if `x` is a PID. Allowed in guards."
|
|
[x])
|
|
(defn is-boolean
|
|
"Returns true if `x` is a boolean. Allowed in guards."
|
|
[x])
|
|
(defn is-nil
|
|
"Returns true if `x` is nil. Allowed in guards."
|
|
[x])
|
|
(defn is-function
|
|
"Returns true if `x` is a function, optionally with given `arity`.
|
|
(is-function f) ;=> true
|
|
(is-function f 2) ;=> true if f takes 2 args"
|
|
([x]) ([x arity]))
|
|
|
|
;; ===== Vectors =====
|
|
(defn vec
|
|
"Converts a collection to a PersistentVector.
|
|
(vec [1 2 3])"
|
|
[coll])
|
|
(defn vector
|
|
"Creates a PersistentVector from arguments.
|
|
(vector 1 2 3)"
|
|
[& args])
|
|
(defn subvec
|
|
"Returns a subvector from `start` to `end`.
|
|
(subvec v 1 3)"
|
|
([v start]) ([v start end]))
|
|
(defn vector?
|
|
"Returns true if `x` is a PersistentVector.
|
|
(vector? (vector 1 2 3)) ;=> true"
|
|
[x])
|
|
|
|
;; ===== Core Data Operations =====
|
|
(defn get
|
|
"Gets value at `key` from collection. Returns `default` if missing.
|
|
(get {:a 1} :a) ;=> 1
|
|
(get {:a 1} :b :not-found) ;=> :not-found"
|
|
([coll key]) ([coll key default]))
|
|
(defn assoc
|
|
"Associates `key` with `val` in collection.
|
|
(assoc {:a 1} :b 2) ;=> {:a 1 :b 2}"
|
|
[coll key val])
|
|
(defn dissoc
|
|
"Dissociates `key` from collection.
|
|
(dissoc {:a 1 :b 2} :a) ;=> {:b 2}"
|
|
[coll key])
|
|
(defn update
|
|
"Updates value at `key` by applying `f`.
|
|
(update {:a 1} :a inc) ;=> {:a 2}"
|
|
([coll key f]) ([coll key f & args]))
|
|
(defn get-in
|
|
"Gets value at nested `path`.
|
|
(get-in {:a {:b 1}} [:a :b]) ;=> 1"
|
|
([coll path]) ([coll path default]))
|
|
(defn assoc-in
|
|
"Associates value at nested `path`.
|
|
(assoc-in {} [:a :b] 1) ;=> {:a {:b 1}}"
|
|
[coll path val])
|
|
(defn update-in
|
|
"Updates value at nested `path` by applying `f`.
|
|
(update-in {:a {:b 1}} [:a :b] inc) ;=> {:a {:b 2}}"
|
|
[coll path f])
|
|
(defn contains?
|
|
"Returns true if `coll` contains `key`.
|
|
(contains? {:a 1} :a) ;=> true"
|
|
[coll key])
|
|
(defn empty?
|
|
"Returns true if `coll` is empty.
|
|
(empty? []) ;=> true"
|
|
[coll])
|
|
(defn nil?
|
|
"Returns true if `x` is nil.
|
|
(nil? nil) ;=> true"
|
|
[x])
|
|
(defn count
|
|
"Returns the number of elements.
|
|
(count [1 2 3]) ;=> 3"
|
|
[coll])
|
|
(defn first
|
|
"Returns the first element.
|
|
(first [1 2 3]) ;=> 1"
|
|
[coll])
|
|
(defn rest
|
|
"Returns all but the first element.
|
|
(rest [1 2 3]) ;=> [2 3]"
|
|
[coll])
|
|
(defn seq
|
|
"Returns a seq on the collection, or nil if empty.
|
|
(seq [1 2 3]) ;=> (1 2 3)"
|
|
[coll])
|
|
(defn conj
|
|
"Adds element to collection (position depends on type).
|
|
(conj [1 2] 3) ;=> [1 2 3]"
|
|
[coll x])
|
|
(defn nth
|
|
"Returns element at `index`.
|
|
(nth [10 20 30] 1) ;=> 20"
|
|
([coll index]) ([coll index not-found]))
|
|
(defn peek
|
|
"Returns the most accessible element.
|
|
(peek [1 2 3]) ;=> 3"
|
|
[coll])
|
|
(defn pop
|
|
"Returns collection without the most accessible element.
|
|
(pop [1 2 3]) ;=> [1 2]"
|
|
[coll])
|
|
|
|
;; ===== Reducing =====
|
|
(defn reduce
|
|
"Reduces collection with `f`.
|
|
(reduce + [1 2 3]) ;=> 6
|
|
(reduce + 0 [1 2 3]) ;=> 6"
|
|
([f coll]) ([f init coll]))
|
|
(defn reduce-kv
|
|
"Reduces a map with `f` receiving (acc, key, value).
|
|
(reduce-kv (fn [acc k v] (+ acc v)) 0 {:a 1 :b 2}) ;=> 3"
|
|
[f init coll])
|
|
|
|
;; ===== Sequence Operations =====
|
|
(defn map
|
|
"Applies `f` to each element, returns a list.
|
|
(map inc [1 2 3]) ;=> [2 3 4]"
|
|
[f coll])
|
|
(defn filter
|
|
"Returns elements where `f` returns truthy.
|
|
(filter (fn [x] (> x 2)) [1 2 3 4]) ;=> [3 4]"
|
|
[f coll])
|
|
(defn concat
|
|
"Concatenates collections.
|
|
(concat [1 2] [3 4]) ;=> [1 2 3 4]"
|
|
[& colls])
|
|
(defn take
|
|
"Returns first `n` elements.
|
|
(take 2 [1 2 3 4]) ;=> [1 2]"
|
|
[n coll])
|
|
(defn drop
|
|
"Drops first `n` elements.
|
|
(drop 2 [1 2 3 4]) ;=> [3 4]"
|
|
[n coll])
|
|
(defn sort
|
|
"Sorts a collection.
|
|
(sort [3 1 2]) ;=> [1 2 3]
|
|
(sort > [3 1 2]) ;=> [3 2 1]"
|
|
([coll]) ([comp coll]))
|
|
(defn sort-by
|
|
"Sorts by the result of `keyfn`.
|
|
(sort-by :name [{:name \"b\"} {:name \"a\"}])"
|
|
([keyfn coll]) ([keyfn comp coll]))
|
|
(defn group-by
|
|
"Groups elements by the result of `f`.
|
|
(group-by even? [1 2 3 4]) ;=> {false [1 3] true [2 4]}"
|
|
[f coll])
|
|
(defn frequencies
|
|
"Returns a map of element → count.
|
|
(frequencies [:a :b :a]) ;=> {:a 2 :b 1}"
|
|
[coll])
|
|
(defn distinct
|
|
"Returns unique elements.
|
|
(distinct [1 2 1 3]) ;=> [1 2 3]"
|
|
[coll])
|
|
(defn mapcat
|
|
"Maps then concatenates results.
|
|
(mapcat (fn [x] [x x]) [1 2 3]) ;=> [1 1 2 2 3 3]"
|
|
[f coll])
|
|
(defn partition
|
|
"Partitions collection into groups of `n`.
|
|
(partition 2 [1 2 3 4]) ;=> [[1 2] [3 4]]"
|
|
([n coll]) ([n step coll]) ([n step pad coll]))
|
|
(defn keys
|
|
"Returns keys of a map.
|
|
(keys {:a 1 :b 2}) ;=> [:a :b]"
|
|
[m])
|
|
(defn vals
|
|
"Returns values of a map.
|
|
(vals {:a 1 :b 2}) ;=> [1 2]"
|
|
[m])
|
|
(defn select-keys
|
|
"Selects only specified `ks` from map.
|
|
(select-keys {:a 1 :b 2 :c 3} [:a :c]) ;=> {:a 1 :c 3}"
|
|
[m ks])
|
|
(defn merge
|
|
"Merges maps. Later maps take precedence.
|
|
(merge {:a 1} {:b 2}) ;=> {:a 1 :b 2}"
|
|
[& maps])
|
|
(defn into
|
|
"Pours elements from `from` into `to`.
|
|
(into {} [[:a 1] [:b 2]]) ;=> {:a 1 :b 2}"
|
|
[to from])
|
|
(defn not=
|
|
"Returns true if arguments are not equal.
|
|
(not= 1 2) ;=> true"
|
|
[& args])
|
|
(defn throw
|
|
"Throws a value (caught by try/catch :throw).
|
|
(throw :some-error)"
|
|
[value])
|