Skip to content

Commit

Permalink
Merge pull request #1 from yurrriq/clj-str
Browse files Browse the repository at this point in the history
Update clj:str
  • Loading branch information
skovsgaard authored Oct 17, 2016
2 parents 478bfab + 2911c19 commit a9fea23
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 60 deletions.
23 changes: 15 additions & 8 deletions doc/lfe_clj.txt
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,21 @@ EXPORTS

Return 'true if x is exactly equal to y.

Other Macros
(str x1, x2 ... xn)

Given arbitrary number of arguments, return a string consisting of each
of their string representations.

N.B. Because Erlang characters are represented as integers, this will
not work for chars, e.g. #\a, which will be presented in the return
value as its integer value, i.e. "97".

> (clj:str #\a "bc")
"97bc"
> (clj:str "a" "bc")
"abc"

Function Composition
(comp f g)

Expand Down Expand Up @@ -602,14 +617,6 @@ EXPORTS

Decrement x by 1.

(str x1, x2 ... xn)

Given arbitrary number of arguments, returns a string consisting of
each of their string representations. N.B. Because Erlang characters
are represented as integers, this will not work for chars, e.g. #\a
which will be presented in the return value as its integer value, e.g.
"97".

AUTHORS
Tim Dysinger, Duncan McGreggor, Eric Bailey.

Expand Down
33 changes: 22 additions & 11 deletions doc/man/lfe_clj.3
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,28 @@ Return \f[C]\[aq]true\f[] if \f[C]x\f[] is less than zero.
\f[B](identical? x)\f[]
.PP
Return \f[C]\[aq]true\f[] if \f[C]x\f[] is exactly equal to \f[C]y\f[].
.SS Other Macros
.PP
\f[B](str x1, x2 ... xn)\f[]
.PP
Given arbitrary number of arguments, return a string consisting of each
of their string representations.
.PP
N.B.
Because Erlang characters are represented as integers, this will not
work for chars, e.g.
\f[C]#\\a\f[], which will be presented in the return value as its
integer value, i.e.
\f[C]"97"\f[].
.IP
.nf
\f[C]
>\ (clj:str\ #\\a\ "bc")
"97bc"
>\ (clj:str\ "a"\ "bc")
"abc"
\f[]
.fi
.SS Function Composition
.PP
\f[B](comp f g)\f[]
Expand Down Expand Up @@ -707,16 +729,5 @@ Increment \f[C]x\f[] by 1.
\f[B](dec x)\f[]
.PP
Decrement \f[C]x\f[] by 1.
.PP
\f[B](str x1, x2 ... xn)\f[]
.PP
Given arbitrary number of arguments, returns a string consisting of each
of their string representations.
N.B.
Because Erlang characters are represented as integers, this will not
work for chars, e.g.
\f[C]#\\a\f[] which will be presented in the return value as its integer
value, e.g.
\f[C]"97"\f[].
.SH AUTHORS
Tim Dysinger, Duncan McGreggor, Eric Bailey.
24 changes: 19 additions & 5 deletions doc/src/lfe_clj.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,25 @@ Return `'true` if `x` is less than zero.
Return `'true` if `x` is exactly equal to `y`.


## Other Macros

**(str x1, x2 ... xn)**

Given arbitrary number of arguments, return a string consisting of each of their
string representations.

N.B. Because Erlang characters are represented as integers, this will not work
for chars, e.g. `#\a`, which will be presented in the return value as its
integer value, i.e. `"97"`.

```lfe
> (clj:str #\a "bc")
"97bc"
> (clj:str "a" "bc")
"abc"
```


## Function Composition

**(comp f g)**
Expand Down Expand Up @@ -650,8 +669,3 @@ Increment `x` by 1.
**(dec x)**

Decrement `x` by 1.

**(str x1, x2 ... xn)**

Given arbitrary number of arguments, returns a string consisting of each of their string representations.
N.B. Because Erlang characters are represented as integers, this will not work for chars, e.g. `#\a` which will be presented in the return value as its integer value, e.g. `"97"`.
4 changes: 4 additions & 0 deletions include/clj.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@
(defmacro pos? args `(clj:pos? ,@args))
(defmacro neg? args `(clj:neg? ,@args))
(defmacro identical? args `(clj:identical? ,@args))

;;; Other macros.

(defmacro str args `(clj:str ,@args))
21 changes: 10 additions & 11 deletions src/clj.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
tuple? atom? binary? bitstring? boolean? bool? float? function? func?
integer? int? number? record? reference? map? undefined? undef? nil?
true? false? odd? even? zero? pos? neg? identical?)
;; String constructor
;; Other macros.
(export-macro str))

(defmacro HAS_MAPS () (quote (erl_internal:bif 'is_map 1)))
Expand Down Expand Up @@ -356,6 +356,15 @@
"Return `'true` if `x` is exactly equal to `y`."
`(=:= ,x ,y))

;;; Other macros.

(defmacro str args
"Construct a string from an arbitrary number of scalar values."
`(lists:flatmap
(lambda (arg)
(clj:cond-> arg
(not (clj:string? arg)) (lfe_io:print1)))
(list ,@args)))

;;; Function composition.

Expand Down Expand Up @@ -643,16 +652,6 @@
"Decrement `x` by 1."
(- x 1))

(defmacro str args
"Construct a string from an arbitrary number of scalar values."
`(lists:flatmap
(lambda (arg)
(clj:cond->
arg
(not (clj:string? arg))
(lfe_io:print1)))
(list ,@args)))

;;; Internal functions.

(defn- -take
Expand Down
53 changes: 28 additions & 25 deletions test/clj-tests.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,34 @@
(is-not (clj:identical? '(a b c) '(a b d)))
(is (clj:identical? '(a b c) '(a b c))))
;;; Other macros.
(deftest str
(are* [x y] (ok? (is-match x y))
"" (clj:str)
"123" (clj:str 1 2 3)
"abc" (clj:str "a" "b" "c")
"abc" (clj:str 'a 'b "c")
"1a2b" (clj:str 1 'a 2 "b")
"2.0c" (clj:str 2.0 'c)
"roughly 3.14"
(let ((pi-string (clj:str "roughly" " " 3.14)))
pi-string)
"200 and a half"
(let ((number-str (clj:str 200 " " 'and " " 'a " " 'half)))
number-str)
"eighty plus 1000"
(let ((x "eighty ")
(y "plus ")
(z 1000))
(clj:str x y z))
"a1b2"
(let ((a 'a)
(b 'b)
(one 1)
(two 2))
(clj:str a one b two))))
;; Based on OTP's queue_SUITE.
(deftest queue?
(is-not (clj:queue? '[1 2 3 4 5]))
Expand Down Expand Up @@ -693,28 +721,3 @@
(deftest dec
(is-match 2 (clj:dec 3))
(is-match 4.0 (clj:dec 5.0)))
(deftest str
(is-match () (clj:str))
(is-match "123" (clj:str 1 2 3))
(is-match "abc" (clj:str "a" "b" "c"))
(is-match "abc" (clj:str 'a 'b "c"))
(is-match "1a2b" (clj:str 1 'a 2 "b"))
(is-match "2.0c" (clj:str 2.0 'c))
(is-match "roughly 3.14"
(let ((pi-string (clj:str "roughly" " " 3.14)))
pi-string))
(is-match "200 and a half"
(let ((number-str (clj:str 200 " " 'and " " 'a " " 'half)))
number-str))
(is-match "eighty plus 1000"
(let ((x "eighty ")
(y "plus ")
(z 1000))
(clj:str x y z)))
(is-match "a1b2"
(let ((a 'a)
(b 'b)
(one 1)
(two 2))
(clj:str a one b two))))

0 comments on commit a9fea23

Please sign in to comment.