Back
apply (clj)
(source)function
(apply f args)
(apply f x args)
(apply f x y args)
(apply f x y z args)
(apply f a b c d & args)
Applies fn f to the argument list formed by prepending intervening arguments to args.
Examples
babashka/babashka
(ns babashka.impl.server
(:require [babashka.impl.clojure.core.server :as server]
[babashka.impl.common :as common]
[babashka.impl.socket-repl :as socket-repl]
[sci.core :as sci]))
(def prepl (fn [& args]
(apply server/prepl (common/ctx) args)))
(def io-prepl
(fn [& args]
(apply server/io-prepl (common/ctx) args)))
(def start-server
(fn [& args]
(apply server/start-server (common/ctx) args)))
noprompt/meander
(ns multimethods
(:refer-clojure :exclude [defmethod defmulti])
(:require
#?(:clj [clojure.core :as clj] :cljs [cljs.core :as cljs])
[meander.epsilon :as m]))
clojure.lang.IFn
(invoke [___ a]
(target-fn a))
(invoke [___ a b]
(target-fn a b))
(invoke [___ a b c]
(target-fn a b c))
(invoke [___ a b c d]
(target-fn a b c d))
(invoke [___ a b c d e]
(target-fn a b c d e))
;; ... apply rest etc
)
mikera/core.matrix
WARNING: because they lack efficient indexed access, sequences will perform badly for most
array operations. In general they should be converted to other implementations before use."
(:require [clojure.core.matrix.protocols :as mp]
[clojure.core.matrix.implementations :as imp]
#?(:clj [clojure.core.matrix.macros :refer [scalar-coerce error]]))
#?(:clj (:import [clojure.lang ISeq])
:cljs (:require-macros [clojure.core.matrix.macros :refer [scalar-coerce error]])))
(extend-protocol mp/PFunctionalOperations
ISeq
(element-seq [m]
(if (== 0 (long (mp/dimensionality (first m))))
m ;; handle 1D case, just return this sequence unchanged
(mapcat mp/element-seq m)))
(element-map
([m f]
(mapv #(mp/element-map % f) m))
([m f a]
(let [[m a] (mp/broadcast-compatible m a)]
(mapv #(mp/element-map % f %2) m (mp/get-major-slice-seq a))))
([m f a more]
(let [[m a & more] (apply mp/broadcast-compatible m a more)] ; FIXME
(mapv #(mp/element-map % f %2 %3) m (mp/get-major-slice-seq a) (map mp/get-major-slice-seq more)))))
(element-map!
([m f]
(error "Sequence arrays are not mutable!"))
([m f a]
(error "Sequence arrays are not mutable!"))
([m f a more]
(error "Sequence arrays are not mutable!")))
(element-reduce
([m f]
(reduce f (mapcat mp/element-seq m)))
([m f init]
(reduce f init (mapcat mp/element-seq m)))))
(extend-protocol mp/PMapIndexed
ISeq
(element-map-indexed
([ms f]
(mapv (fn [i m] (mp/element-map-indexed m #(apply f (cons i %1) %&)))
(range (count ms)) ms))
([ms f as]
(let [[ms as] (mp/broadcast-compatible ms as)]
(mapv (fn [i m a]
(mp/element-map-indexed m #(apply f (cons i %1) %&) a))
(range (count ms)) ms (mp/get-major-slice-seq as))))
([ms f as more]
(let [[ms as & more] (apply mp/broadcast-compatible ms as more)] ; FIXME
(mapv (fn [i m a & mr]
(mp/element-map-indexed m #(apply f (cons i %1) %&) a mr))
(range (count ms)) ms
(mp/get-major-slice-seq as)
(map mp/get-major-slice-seq more)))))
(element-map-indexed!
([m f]
(error "Sequence arrays are not mutable!"))
([m f a]
(error "Sequence arrays are not mutable!"))
([m f a more]
(error "Sequence arrays are not mutable!"))))
hraberg/deuce
(ns deuce.emacs.eval
(:use [deuce.emacs-lisp :only (defun defvar)])
(:require [clojure.core :as c]
[deuce.emacs.alloc :as alloc]
[deuce.emacs.data :as data]
[deuce.emacs-lisp.cons :as cons]
[deuce.emacs-lisp :as el])
(:import [clojure.lang Var])
(:refer-clojure :exclude [apply eval macroexpand]))
(defvar debugger nil
"Function to call to invoke debugger.
If due to frame exit, args are `exit' and the value being returned;
this function's value will be returned instead of that.
If due to error, args are `error' and a list of the args to `signal'.
If due to `apply' or `funcall' entry, one arg, `lambda'.
If due to `eval' entry, one arg, t.")
(defvar max-lisp-eval-depth nil
"*Limit on depth in `eval', `apply' and `funcall' before error.
(defvar debug-on-next-call nil
"Non-nil means enter debugger before next `eval', `apply' or `funcall'.")
(defvar debug-ignored-errors nil
"*List of errors for which the debugger should not be called.
Each element may be a condition-name or a regexp that matches error messages.
If any element applies to a given error, that error skips the debugger
and just returns to top level.
This overrides the variable `debug-on-error'.
It does not apply to errors handled by `condition-case'.
(defvar debug-on-quit nil
"*Non-nil means enter debugger if quit is signaled (C-g, for example).
Does not apply if quit is handled by a `condition-case'.
(defvar debug-on-error nil
"*Non-nil means enter debugger if an error is signaled.
Does not apply to errors handled by `condition-case' or those
matched by `debug-ignored-errors'.
If the value is a list, an error only means to enter the debugger
if one of its condition symbols appears in the list.
When you evaluate an expression interactively, this variable
is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
The command `toggle-debug-on-error' toggles this.
See also the variable `debug-on-quit'.
(declare apply eval funcall)
(defun autoload (function file &optional docstring interactive type)
"Define FUNCTION to autoload from FILE.
FUNCTION is a symbol; FILE is a file name string to pass to `load'.
Third arg DOCSTRING is documentation for the function.
Fourth arg INTERACTIVE if non-nil says function can be called interactively.
Fifth arg TYPE indicates the type of the object:
nil or omitted says FUNCTION is a function,
`keymap' says FUNCTION is really a keymap, and
`macro' or t says FUNCTION is really a macro.
Third through fifth args give info about the real definition.
They default to nil.
If FUNCTION is already defined other than as an autoload,
this does nothing and returns nil."
(when (or (not (el/fun function)) (-> (el/fun function) meta :autoload))
(let [macro? (= 'macro type)
autoload-symbol (fn autoload-symbol [function]
(let [f (el/fun function)]
(when (-> f meta :autoload)
(ns-unmap 'deuce.emacs (el/sym function))
((el/fun 'load) (-> f meta :file) nil true))))
definition (if macro?
(fn autoload-macro [&form &env & args] ;; Note implicit macro args, see defalias
(do
(autoload-symbol function)
`(el/progn (~(el/sym function) ~@args))))
(fn autoload [& args]
(autoload-symbol function)
(c/apply (el/fun function) args)))] ;; el->clj?
(ns-unmap 'deuce.emacs function)
(el/defvar-helper* 'deuce.emacs function definition docstring)
(alter-meta! (el/fun function) merge {:autoload true :file file} (when interactive {:interactive nil}))
(when macro? (.setMacro ^Var (el/fun function))))
function))
Do not use `make-local-variable' to make a hook variable buffer-local.
Instead, use `add-hook' and specify t for the LOCAL argument."
(when-let [hook (el/global hook)]
(let [hook @hook]
(doall (map #(c/apply funcall % args) (if (fn? hook) [hook] hook))))))
(defun funcall (function &rest arguments)
"Call first argument as a function, passing remaining arguments to it.
Return the value that function returns.
Thus, (funcall 'cons 'x 'y) returns (x . y)."
(apply function arguments))
Do not use `make-local-variable' to make a hook variable buffer-local.
Instead, use `add-hook' and specify t for the LOCAL argument."
(some identity (apply run-hook-with-args hook args)))
Do not use `make-local-variable' to make a hook variable buffer-local.
Instead, use `add-hook' and specify t for the LOCAL argument."
(or (some (complement identity) (apply run-hook-with-args hook args))
true))
(defun apply (function &rest arguments)
"Call FUNCTION with our remaining args, using our last arg as list of args.
Then return the value FUNCTION returns.
Thus, (apply '+ 1 2 '(3 4)) returns 10."
(let [rest (last arguments)]
(el/check-type 'listp rest)
(c/apply (cond (symbol? function) (data/symbol-function function)
(data/listp function) (eval function)
:else function) (c/apply alloc/list (concat (butlast arguments) rest)))))
hraberg/deuce
(ns deuce.emacs.callproc
(:use [deuce.emacs-lisp :only (defun defvar) :as el])
(:require [clojure.core :as c]
[clojure.string :as s]
[clojure.java.io :as io]
[clojure.java.shell :as sh]
[deuce.emacs.buffer :as buffer]
[deuce.emacs.data :as data]
[deuce.emacs.editfns :as editfns])
(:import [java.io File])
(:refer-clojure :exclude []))
(defvar exec-path (apply list (.split (System/getenv "PATH") File/pathSeparator))
"*List of directories to search programs to run in subprocesses.
Each element is a string (directory name) or nil (try default directory).
(defvar initial-environment (apply list (map str (System/getenv)))
"List of environment variables inherited from the parent process.
Each element should be a string of the form ENVVARNAME=VALUE.
The elements must normally be decoded (using `locale-coding-system') for use.")
(defvar process-environment (apply list (map (partial s/join "=") (into {} (System/getenv))))
"List of overridden environment variables for subprocesses to inherit.
Each element should be a string of the form ENVVARNAME=VALUE.
If BUFFER is 0, `call-process' returns immediately with value nil.
Otherwise it waits for PROGRAM to terminate
and returns a numeric exit status or a signal description string.
If you quit, the process is killed with SIGINT, or SIGKILL if you quit again."
(let [opts (if infile [:in (io/file infile)] [])
no-wait? (= 0 buffer)
buffer (or no-wait?
(and (data/consp buffer) (= :file (data/car buffer)) (data/cdr buffer))
(and (true? buffer) (buffer/current-buffer))
(el/check-type 'bufferp (or (when (data/consp buffer) (data/car buffer))
buffer (buffer/current-buffer))))
stderr (when (data/consp buffer) (data/cdr buffer))
runner (if no-wait? #(do (future-call %) nil) #(%))]
(runner #(let [{:keys [exit out err]}
(apply sh/sh (concat (cons program args) opts))]
(when (data/bufferp buffer)
(binding [buffer/*current-buffer* buffer]
(editfns/insert out)
(when (true? stderr)
(editfns/insert err))))
(when (string? buffer)
(spit (io/file buffer) out))
(when (string? stderr)
(spit (io/file stderr) err))
exit))))