Files
2026-03-09 23:09:46 -04:00

559 lines
10 KiB
Clojure

(ns erlang
"Erlang :erlang module — BEAM runtime BIFs (Built-In Functions).
In CljElixir: (erlang/self), (erlang/band x y), etc.
These are the lowest-level BEAM operations, many are used internally.")
;; --- Process ---
(defn self
"Returns the PID of the calling process.
(erlang/self) ;=> #PID<0.123.0>"
[])
(defn spawn
"Spawns a new process. Returns PID.
(erlang/spawn (fn [] (do-work)))
(erlang/spawn Module :fun [args])"
([fun])
([module function args]))
(defn spawn-link
"Spawns and links a process."
([fun])
([module function args]))
(defn spawn-monitor
"Spawns and monitors a process. Returns {pid ref}."
([fun])
([module function args]))
(defn send
"Sends a message. Same as `!` operator.
(erlang/send pid :hello)"
([dest msg])
([dest msg opts]))
(defn exit
"Sends an exit signal.
(erlang/exit :normal)
(erlang/exit pid :kill)"
([reason])
([pid reason]))
(defn link
"Creates a bidirectional link.
(erlang/link pid)"
[pid])
(defn unlink
"Removes a link.
(erlang/unlink pid)"
[pid])
(defn monitor
"Starts monitoring a process.
(erlang/monitor :process pid)"
[type item])
(defn demonitor
"Stops monitoring.
(erlang/demonitor ref)"
([ref])
([ref opts]))
(defn process-flag
"Sets process flags.
(erlang/process-flag :trap-exit true)"
([flag value])
([pid flag value]))
(defn process-info
"Returns info about a process.
(erlang/process-info pid)
(erlang/process-info pid :message-queue-len)"
([pid])
([pid item]))
(defn processes
"Returns a list of all process PIDs."
[])
(defn is-process-alive
"Returns true if `pid` is alive."
[pid])
(defn register
"Registers a process under a name.
(erlang/register :my-server (erlang/self))"
[name pid])
(defn unregister
"Unregisters a name."
[name])
(defn whereis
"Returns PID for registered name, or :undefined."
[name])
(defn registered
"Returns list of all registered names."
[])
(defn group-leader
"Gets or sets the group leader.
(erlang/group-leader)
(erlang/group-leader new-leader pid)"
([])
([leader pid]))
(defn hibernate
"Puts process in hibernate mode (frees heap)."
[module function args])
;; --- Bitwise Operations ---
(defn band
"Bitwise AND.
(erlang/band 0xFF 0x0F) ;=> 15"
[int1 int2])
(defn bor
"Bitwise OR.
(erlang/bor 0x0F 0xF0) ;=> 255"
[int1 int2])
(defn bxor
"Bitwise XOR.
(erlang/bxor 0xFF 0x0F) ;=> 240"
[int1 int2])
(defn bnot
"Bitwise NOT.
(erlang/bnot 0) ;=> -1"
[int])
(defn bsl
"Bitwise shift left.
(erlang/bsl 1 5) ;=> 32"
[int shift])
(defn bsr
"Bitwise shift right.
(erlang/bsr 32 5) ;=> 1"
[int shift])
;; --- Arithmetic ---
(defn abs
"Returns absolute value.
(erlang/abs -5) ;=> 5"
[number])
(defn div
"Integer division (truncated towards zero).
(erlang/div 10 3) ;=> 3"
[a b])
(defn rem
"Integer remainder.
(erlang/rem 10 3) ;=> 1"
[a b])
(defn float
"Converts to float.
(erlang/float 42) ;=> 42.0"
[number])
(defn round
"Rounds to nearest integer.
(erlang/round 3.5) ;=> 4"
[number])
(defn trunc
"Truncates to integer.
(erlang/trunc 3.9) ;=> 3"
[number])
(defn ceil
"Ceiling (smallest integer >= number).
(erlang/ceil 3.1) ;=> 4"
[number])
(defn floor
"Floor (largest integer <= number).
(erlang/floor 3.9) ;=> 3"
[number])
;; --- Tuple Operations ---
(defn element
"Gets element at 1-based `index` from tuple.
(erlang/element 1 #el[:a :b :c]) ;=> :a"
[index tuple])
(defn setelement
"Sets element at 1-based `index` in tuple.
(erlang/setelement 1 #el[:a :b] :x) ;=> #el[:x :b]"
[index tuple value])
(defn tuple-size
"Returns the size of a tuple.
(erlang/tuple-size #el[1 2 3]) ;=> 3"
[tuple])
(defn make-tuple
"Creates a tuple of `arity` filled with `init-value`.
(erlang/make-tuple 3 0) ;=> #el[0 0 0]"
([arity init-value])
([arity default-value init-list]))
(defn append-element
"Appends `element` to `tuple`.
(erlang/append-element #el[1 2] 3) ;=> #el[1 2 3]"
[tuple element])
(defn tuple-to-list
"Converts a tuple to a list.
(erlang/tuple-to-list #el[1 2 3]) ;=> [1 2 3]"
[tuple])
(defn list-to-tuple
"Converts a list to a tuple.
(erlang/list-to-tuple [1 2 3]) ;=> #el[1 2 3]"
[list])
;; --- Type Conversion ---
(defn atom-to-list
"Converts atom to charlist.
(erlang/atom-to-list :hello) ;=> 'hello'"
[atom])
(defn list-to-atom
"Converts charlist to atom."
[charlist])
(defn atom-to-binary
"Converts atom to binary string.
(erlang/atom-to-binary :hello) ;=> \"hello\""
([atom])
([atom encoding]))
(defn binary-to-atom
"Converts binary to atom."
([binary])
([binary encoding]))
(defn binary-to-existing-atom
"Converts binary to existing atom."
([binary])
([binary encoding]))
(defn integer-to-list
"Converts integer to charlist.
(erlang/integer-to-list 123) ;=> '123'
(erlang/integer-to-list 255 16) ;=> 'FF'"
([integer])
([integer base]))
(defn list-to-integer
"Converts charlist to integer."
([charlist])
([charlist base]))
(defn integer-to-binary
"Converts integer to binary string.
(erlang/integer-to-binary 123) ;=> \"123\""
([integer])
([integer base]))
(defn binary-to-integer
"Converts binary to integer."
([binary])
([binary base]))
(defn float-to-list
"Converts float to charlist."
([float])
([float opts]))
(defn float-to-binary
"Converts float to binary."
([float])
([float opts]))
(defn list-to-float
"Converts charlist to float."
[charlist])
(defn binary-to-float
"Converts binary to float."
[binary])
(defn binary-to-list
"Converts binary to list of bytes.
(erlang/binary-to-list \"hello\") ;=> [104 101 108 108 111]"
([binary])
([binary start stop]))
(defn list-to-binary
"Converts iolist to binary.
(erlang/list-to-binary [104 101 108 108 111]) ;=> \"hello\""
[iolist])
(defn iolist-to-binary
"Converts iolist to binary.
(erlang/iolist-to-binary [\"hello\" \" \" \"world\"]) ;=> \"hello world\""
[iolist])
(defn iolist-size
"Returns byte size of an iolist."
[iolist])
(defn term-to-binary
"Serializes a term to binary (External Term Format).
(erlang/term-to-binary {:key \"value\"}) ;=> <<131, ...>>"
([term])
([term opts]))
(defn binary-to-term
"Deserializes binary (ETF) to a term.
(erlang/binary-to-term bin) ;=> {:key \"value\"}"
([binary])
([binary opts]))
;; --- Type Checks ---
(defn is-atom
"Returns true if term is an atom."
[term])
(defn is-binary
"Returns true if term is a binary."
[term])
(defn is-boolean
"Returns true if term is a boolean."
[term])
(defn is-float
"Returns true if term is a float."
[term])
(defn is-function
"Returns true if term is a function."
([term])
([term arity]))
(defn is-integer
"Returns true if term is an integer."
[term])
(defn is-list
"Returns true if term is a list."
[term])
(defn is-map
"Returns true if term is a map."
[term])
(defn is-map-key
"Returns true if key exists in map. Guard-safe."
[map key])
(defn is-number
"Returns true if term is a number."
[term])
(defn is-pid
"Returns true if term is a PID."
[term])
(defn is-port
"Returns true if term is a port."
[term])
(defn is-reference
"Returns true if term is a reference."
[term])
(defn is-tuple
"Returns true if term is a tuple."
[term])
;; --- List Operations ---
(defn hd
"Returns the head of a list.
(erlang/hd [1 2 3]) ;=> 1"
[list])
(defn tl
"Returns the tail of a list.
(erlang/tl [1 2 3]) ;=> [2 3]"
[list])
(defn length
"Returns the length of a list.
(erlang/length [1 2 3]) ;=> 3"
[list])
;; --- Binary/Bitstring ---
(defn byte-size
"Returns byte size of a binary.
(erlang/byte-size \"hello\") ;=> 5"
[binary])
(defn bit-size
"Returns bit size of a bitstring."
[bitstring])
(defn binary-part
"Extracts a part of a binary. Guard-safe.
(erlang/binary-part \"hello\" 1 3) ;=> \"ell\""
[binary start length])
(defn split-binary
"Splits binary at position.
(erlang/split-binary \"hello\" 3) ;=> {\"hel\" \"lo\"}"
[binary pos])
;; --- Hash ---
(defn phash2
"Portable hash function. Returns integer in 0..(range-1).
(erlang/phash2 :my-term) ;=> 12345678
(erlang/phash2 :my-term 100) ;=> 78"
([term])
([term range]))
;; --- Time ---
(defn monotonic-time
"Returns monotonic time.
(erlang/monotonic-time) ;=> native time units
(erlang/monotonic-time :millisecond)"
([])
([unit]))
(defn system-time
"Returns system (wall-clock) time."
([])
([unit]))
(defn unique-integer
"Returns a unique integer.
(erlang/unique-integer [:positive :monotonic])"
([])
([modifiers]))
(defn timestamp
"Returns {megasecs secs microsecs} tuple."
[])
(defn convert-time-unit
"Converts time between units.
(erlang/convert-time-unit time :native :millisecond)"
[time from-unit to-unit])
;; --- Node ---
(defn node
"Returns the current node name.
(erlang/node) ;=> :nonode@nohost"
([])
([arg]))
(defn nodes
"Returns list of connected nodes.
(erlang/nodes) ;=> [:node1@host ...]"
([])
([type]))
(defn is-alive
"Returns true if the node is part of a distributed system."
[])
;; --- System ---
(defn apply
"Applies function. Like Kernel.apply.
(erlang/apply Enum :map [[1 2 3] inc])
(erlang/apply fun args)"
([fun args])
([module function args]))
(defn error
"Raises an error.
(erlang/error :badarg)
(erlang/error {:my-error \"reason\"})"
([reason])
([reason args]))
(defn throw
"Throws a term (caught by try/catch :throw)."
[term])
(defn halt
"Halts the runtime.
(erlang/halt 0)"
([])
([status])
([status opts]))
(defn garbage-collect
"Forces garbage collection on current (or specified) process.
(erlang/garbage-collect)"
([])
([pid]))
(defn memory
"Returns memory usage info.
(erlang/memory) ;=> [{:total N} {:processes N} ...]
(erlang/memory :total) ;=> bytes"
([])
([type]))
(defn system-info
"Returns system information.
(erlang/system-info :process-count)
(erlang/system-info :otp-release)"
[item])
(defn statistics
"Returns runtime statistics.
(erlang/statistics :wall-clock)"
[type])
(defn make-ref
"Creates a unique reference."
[])
(defn md5
"Computes MD5 hash of binary. Returns 16-byte binary."
[data])
(defn crc32
"Computes CRC32 checksum."
([data])
([old-crc data]))
(defn adler32
"Computes Adler-32 checksum."
([data])
([old-adler data]))
;; --- Map Operations ---
(defn map-get
"Gets a value from a map. Guard-safe.
(erlang/map-get :key my-map)"
[key map])
(defn map-size
"Returns the number of entries in a map. Guard-safe.
(erlang/map-size {:a 1 :b 2}) ;=> 2"
[map])