Mew is a library targetting R5RS/R7RS scheme, which provides some conveniences inspired from Goo, Clojure, T and Arc to allow writing more compact code. == Design goals Mew, in its current iteration, is designed as a Scheme library. Even though a program written in Mew will look quite different to a plain Scheme program, code is compatible, and Scheme code can generally live inside a Mew program, and Mew can load Scheme libraries. Extra care has been taken to not reuse identifiers found in important SRFIs. Mew code is tight code, commonly used Mew identifiers are short. Mew uses a few features provided by R7RS internally, but does not require using R7RS. Mew targets Chicken Scheme currently but should be easy to port to other Scheme implementations. Mew is multi-paradigm: it simplifies both writing imperative and functional code. Mew uses SRFI-158 (Generators and Accumulators) for generic functions on enumerable structures. Mew generally uses {{equal?}} as equality predicate. Mew never mutates lists. Except for few shortcut macros ({{op}} and {{op*}), Mew endorses macro hygiene. == Re-Exports Mew re-exports SRFI-1 (List library), SRFI-13 (String Libraries) (from {{utf8-srfi-13}}), SRFI-69 (Basic hash tables), SRFI-158 (Generators and Accumulators), {{(chicken io)}}, {{(chicken irregex)}}, {{(chicken pretty-print)}}}, {{matchable}}, and {{err}} (see {{err.svnwiki}}). == Definitions, bindings and assignments (def ) (def ( ) ) Alias for {{define}}. (fun ...) Alias for {{lambda}}. (fun* ...) Lambda which pattern matches all its arguments (ala the {{match}} macro). (loc ( ... ) ) Binds local variables in pattern (ala the {{match}} macro) {{pat1}} to the result of evaluating {{val1}}, then {{pat2}} to {{val2}}, etc., then evaluates {{body}}. Assignments can refer to previously assigned values of the {{loc}}. (andloc ( ... ) ) Like {{loc}} (without pattern matching), but return false when one of the {{>}} evaluates to false. If a {{}} is {{_}}, only performs the check without binding a value. (rec ) (rec ( ) ) See Module%20(chicken%20base)#rec or SRFI-31. Often, using {{def}} in an inner scope is preferable to using {{rec}}. (set ) Like {{set!}}, but also return the value of {{}}. == Control flow (esc ) Bind {{}} to the current continuation, then run {{}}. Code should not pass {{}} outside the lexical scope and only call it once---use {{call/cc}} else. (fin . ) Evaluate {{}}, then evaluate {{}}, even if {{}} used a non-local exit (as from {{esc}}). Returns value of {{}}, so can also be used as a replacment for Common Lisp PROG1. (rep ( ... ) ...) Explicit form of named {{let}}, using {{loc}} binding syntax. Assignments cannot refer to previously assigned values of the {{rep}}. (seq . ) Like {{begin}}, but allows {{}} to be empty and returns a value which is {{void?}} then. (unless ...) (when ...) As in R7RS-small. (while ...) (until ...) Evaluate {{}} while {{}} is true/false. (xcond ...) Like {{cond}}, but raise error if no case matched. (fail ? ...) Create and signal a condition of {{}} (default: {{(exn)}}) with a {{'message}} of {{}} passed through {{format}} with {{}}. == Numeric helpers (inc ) (dec ) Increment or decrement the argument by 1. (inc! []) (dec! []) Increment or decrement the location {{}} by {{}} (default: 1). (div ) Alias for {{floor-quotient}}. (mod ) Alias for {{floor-remainder}}. (sgn ) Returns -1, 0, 1 depending on whether {{}} is negative, zero, or positve. == General helpers (app . ) Like {{apply}}, but supports empty {{}}. (op
) Returns a procedure that evaluates {{}} with {{_}} bound to its only argument. If {{>} is empty, behaves as {{values}}, i.e. the identity function. {{(op 5)}} is the function that always returns 5. {{(op + 2 _)}} is the function that adds 2 to its argument. Note that {{(op (expr))}} can be used as a generator that infinintely often evaluates {{(expr)}} anew. (op* ) Returns a procedure that evaluates {{}} with {{...}} expanded to all its arguments. {{...}} must only appear once and must not be nested further. If {{...} does not appear in {{}}, it's added implicitly at the end. {{(op* + 5)}} is the function that adds 5 and then all its arguments. {{(op* - 0 ... 2)}} is the function that subtracts all its arguments from 0 and finally 2. (proj ...) Expands to a function that returns its {{}}th argument (zero indexed). Multiple arguments are returned as multiple values. (cr ) The single argument identity function. Generalizes the {{c.*r}} family. (boolean ) Return false if {{}} is {{#f}}, and true else. (negate ) Alias for {{complement}}. (comp ...) Alias for {{compose}}. (per ...) Reverse function composition. (act ...) Reverse function compose all {{}}, then apply to {{}}. (=> ...) Alias for {{act}}. (=>* ...) Like {{=>}}, but {{}} may return multiple values. (set=> ...) Shortcut for {{(set (=> ...))}}. (and=> ...) Apply the first function in {{}} to {{}}, then the second to the result, etc. Stop if any value is false. (op=> ...) Like {{=>}}, but all {{}} which are lists implicitly use {{op}}. If {{}} is not a list, an unquoted {{,}} list, or a list not containing {{_}} directly, it is used as is. (fun=> ...) Like {{per}}, but all {{}} which are lists implicitly use {{op}}. If {{}} is not a list, or an unquoted {{,}} list, it is used as is. (juxt ...) Returns a function that applies each {{}} to its arguments and returns the results as multiple values. (unlist ) Returns the list {{}} as multiple values. (str . ) Returns a new string composed by concatenating the strings given by applying {{display}} to all {{}}. (one-of ...) Expands to a lambda expression that is true if its argument is {{equal?}} to any of the {{}} passed. (sing? ) Return true iff {{}} is a proper list of length 1. (void ...) Ignores all arguments and returns a value where {{void?}} is true. (void? ) Returns true if {{}} is an unspecified value, else false. (scan ...) (scan-right ...) Like {{fold}}/{{fold-right}}, but collects all accumulator values. Prefer {{xscan}}/{{xscan-right}}. (xfold ...) (xfold-right ...) (xreduce ...) (xreduce-right ...) (xscan ...) (xscan-right ...) Like {{fold}}/{{fold-right}}/{{reduce}}/{{reduce-right}}/{{scan}}/{{scan-right}}, but {{}} always takes the accumulator as first arguments, and the items after. This is more practical when multiple {{lists}} are passed. (imp ... ) Material implication: evaluate {{...}} until one is false, then shortcut and return true. If all {{...}} are true, evaluate {{}}. (push! ) Prepend {{}} to the list stored at {{}}. (pop! []) Return the head of {{}} and replace {{}} with its tail. If {{}} is empty, return {{}} if given; else throw an exception. == I/O helpers (prn . ) {{write}} all {{args}} separated by spaces and terminated by a newline to the current output stream. Returns the value of the last argument. (puts . ) {{display}} all {{args}} terminated by a newline to the current output stream. (eof) Return an object for which {{eof-object?}} is true. (slurp ) If {{}} is an input-port, read all data into a string. If {{}} is a string, read all data from the file named {{<>}. If {{}} is false, read all data from {{*current-input-port*}}. (lines []) If {{}} is missing, return {{read-line}} (a generator that reads lines from {{*current-input-port*}}). If {{}} is an input-port, return a generator that reads lines from {{}}. If {{}} is a string, return a generator that reads lines from the file named {{<>} and closes it on EOF. == Equality (=? ...) Return true if all values are {{equal?}} or hash-tables with same set of keys and {{equal?}} values. (<>? ...) Return true if all values are pairwise different. == Generic comparison (cmp ) Compare the real/char/string/list/vector {{}} to {{}} and return -1 if {{}} is less than {{}}, 0 if {{}} is equal to {{}} 1 if {{}} is greater than {{}}. Return false if {{}} and {{}} cannot be compared. Lists and vectors are compared in lexicographic order using {{cmp}} recursively. ( ...) (>? ...) (<=? ...) (>=? ...) Return true if all arguments are monotonically increasing, monotonically decreasing, monotonically non-decreasing, or monotonically non-increasing according to {{cmp}}, and false otherwise. It is an error to compare uncomparable values. == Sorting (sort []) Sort the list/vector {{}} according to the relation {{}} (by default: {{(sort! []) Sort the vector {{}} destructively according to the relation {{?}} (by default: {{(sort-by []) Sort the items of the list/vector {{}} by their image under {{f}}, according to the relation {{}} (by default: {{(get ) (at ) (set (at ) ) Generalized accessor, supports indexing into lists, vectors, hash-tables, strings. (set-at ...) Mutate the hash-table/vector/string {{}} by setting the key/index {{}} to {{}} etc. Returns {{}}. (del-at ...) Delete {{}} from the hash-table, and return the hash-table. (tbl ... ) Construct a hash-table; using {{equal?}}. (keys ) Alias for {{hash-table-keys}}. (vals ) Alias for {{hash-table-values}}. (empty? ) Test if {{}} is an empty list/string/vector/hash-table. (len ) Return the length of the list/vector/string/hash-table/generator {{}}. (len>= ) Return true if the list/vector/string/hash-table/generator {{}} has at least {{}} elements. This is more efficient for lists and generators than {{len}} and works on infinite structures. (dup []) Return a duplicate of the nested datastructure {{}}, consisting of lists, vectors, hash-tables and strings. Create copies of values up to a level of {{}}, or infinitely by default. (for ( ...) ...) (for (( . ) ...) ...) (for/into ( ...) ...) If {{}} is a list or a vector, iterate over its elements. If {{}} is a procedure, consider it a SRFI-158 generator and iterate over its values. If {{}} is a hash-table, iterate over its keys and values. Multiple {{ }}-pairs may be specified, then {{for}} iterates over these in *parallel*, stopping after the shortest ends. The variant {{for/into}} accumulates the values of the {{}} and returns the result of the accumulation. (search ?) Returns the offset of the sequence (string/list/vector) {{}} in the sequence {{}}, starting no earlier than {{>}} (default: 0). Returns false if the sequence cannot be found. (repeat ) Repeat the list/vector/string/char {{}} {{}} times. == Generators and Accumulators (gen ) Generic generator for list/vector/string/hash-table/generator. (cycle ...) Alias for {{circular-generator}}. (range ? ? ?) A variant of {{make-range-generator}} that also allows negative {{}}. {{}} defaults to zero. {{}} defaults to infinity. {{}} defaults to 1. (giterate ) Generator returning {{}}, {{( )}}, {{( ()))}}... (gconcatenate ) Generator yielding all values of the all generators yielded by {{}}. (gpick ) Like {{gmap}}, apply {{}} to all values yielded by the generator {{}}, but skip values when {{}} returns an eof object. (gwindow ) Generator yielding a sliding window of length {{}} (as a list) over the values yielded by the generator {{}}. Yields never if the generator yielded fewer than {{}} elements. (gsplit-on ) Partition the elements yielded by the generator {{}} into lists: starts a new empty list when the predicate {{}} called with the current element of the generator returns true. In this case, the element is discarded. (gslice-when ) Partition the elements yielded by the generator {{}} into lists: starts a new list when the predicate {{}} called with the previous and the current element of the generator returns true. (genumerate ) Takes the values yielded by the generator {{}} and yields them as a {{cons}} where the first cell is an index incremented on every yield. (gfix ) Returns a generator that runs the generator {{}} until it yields a value {{equal?}} to the preceding one, then stops. (final ) Run the generator {{}} until it stops and return its final value. (into ...) Feed all elements of the generators {{}} into the accumulator {{}}. Uses {{gen}} to convert {{}} into a generator. If {{}} is a list/vector/hash-table/string, accumulate into a corresponding accumulator initialized to {{}}. (accumulate ( ) body ...) Bind {{}} to the accumulator {{}} and run {{body}}. Finally, return the result of accumulation. If {{}} is a list/vector/hash-table/string, accumulate into a corresponding accumulator initialized to {{}}. (tally-accumulator) Returns an accumulator that counts how often each element was accumulated. The accumulator results in a hash-table of objects to numbers. (group-by-accumulator ) Returns an accumulator that stores all elements in lists in a hash-table, applying {{}} to the element to compute the key. (uniq-accumulator ?) Returns an accumulator that returns a list of unique elements. Two elements are considered equal if their image under {{}} is {{equal?}}. {{}} defaults to the identity function. (nth-accumulator ) Returns an accumulator that saves the {{}}-th element, or an void value else. (inject-accumulator []) Returns an accumulator that xfolds {{}} over the elements. If given, folding starts with {{}}, else with the first element received. (generator-xfold ...) Like {{generator-fold}}, but {{}} always takes the accumulator as first arguments, and the items after. This is more practical when multiple {{}} are passed. (inject ? ?) Returns a procedure that takes an generator (or something convertible by {{gen}}) and xfolds the function {{}} over its values. If given, folding starts with {{}}, else with the first element yielded by the generator. If the generator is empty, return {{()}}. If {{}} is passed, immediately fold over the generator {{}}. (odometer ...) Returns a generator that takes a list of numbers and yields all combinations of numbers such that each is below it's wheel. The rightmost number changes most quickly. (cross-product ...) Returns a generator that takes multiple lists/vectors/strings and yields all elements of their cartesian product (as lists). == Regular expressions (?~ ) Matches the string {{}} against the irregex (string or sexp) {{}} without anchoring. Returns false on no match, else a list of all match data strings. (gmatch ) Returns a generator that for each match of the irregex {{}} in the string {{}} either yields the match, or a list of all match data strings (if there are any). (gsplit ?) Returns a generator that yields the strings between matches of the irregex {{}} in the string {{}}, at most {{>} times (by default, unlimited). When the pattern {{}} uses match data, the result is unspecified. == Random numbers Mew initializes the {{(chicken random)}} generator from a high entropy source. (rand) Returns a random real between 0 and 1. (rand ) Returns a random integer such that 0 <= {{(rand )}} < {{}}. (rand ) Returns a random integer such that N <= {{(rand )}} < {{}}. (shuffle ) Returns a copy of the vector {{}} with the entries in randomized order. (shuffle! ) Shuffles the vector {{}} randomly in-place using a Fisher-Yates shuffle. (sample ) Returns a random element of the list/vector/string {{}}. Returns a random key/value pair of the hash-table {{}}. (sample ) Returns a random list/vector/string consisting of up to {{}} elements of the list/vector/string {{}}, without replacement. Returns a random hash-table consisting of up to {{}} key/value pairs of the hash-table {{}}, without replacement. == Special syntax (-> a -> b c -> d e f) (-> a ->> b c ->> d e f) Nesting macros: {{->}} inserts the previous part as the second argument: {{(-> a -> b c -> d e f)}} expands to {{(d (b a c) e f)}}. {{->>}} inserts the previous part as the last argument: {{(-> a ->> b c ->> d e f)}} expands to {{(d e f (b c a))}}. You can mix {{->}} and {{->>}} macros: {{(-> a -> b c ->> d e f)}} expands to {{(d e f (b a c))}}. Nesting macros must start off with a {{->}}. (fun-> b c -> d e f) (fun->> b c ->> d e f) Nesting lambdas: like {{->}} but the nesting starts with the argument of the lambda. {{(fun-> b c -> d e f)}} expands to {{(lambda (x) (-> x -> b c -> d e f))}}. {{(fun->> b c ->> d e f)}} expands to {{(lambda (x) (-> x ->> b c ->> d e f))}}. (set-> loc ...) Mutation with nesting macros: shortcut for {{(set loc (-> loc ...))}}. (-> ... if-> ?) Evaluate {{}}. Then, when {{}} is not false, behaves like {{(-> -> }}, otherwise like {{(-> -> )}} (or just {{}} if no {{}} was passed). (-> ... and-> ...) (-> ... and->> ...) Like {{->}}/{{->>}} but skips nesting the code if the nested expression is false. (-> ... ok-> ...) (-> ... ok->> ...) Like {{->}}/{{->>}} but skips nesting the code if the nested expression is {{err?}}. (-> ... err-> ...) (-> ... err->> ...) Like {{->}}/{{->>}} but skips nesting the code if the nested expression is {{ok?}}. The unwrapped value is inserted into the nesting.