├── .gitignore ├── Readme.md ├── UNLICENSE ├── pipe.nim ├── pipe.nimble └── tests ├── nim.cfg └── test.nim /.gitignore: -------------------------------------------------------------------------------- 1 | nimcache/ 2 | tests/test 3 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Pipe # 2 | 3 | [![license]](License.md) 4 | 5 | A pipe operator for nim, as seen in functional languages. 6 | 7 | ## Origins ## 8 | 9 | I was looking for a good old `|>` to use in nim. 10 | I found [this](https://github.com/jaym/nim-pipeline). 11 | It sucked. It wasn't published. It used "example" in its description. 12 | 13 | So I decided to write my own, with blackjack and hookers. 14 | 15 | ## Examples ## 16 | 17 | Please take a look in tests/test.nim for examples, but here's a tl;dr: 18 | 19 | ```nim 20 | 1 |> `+`(2) |> foo 21 | # is equivalent to 22 | foo(1+2) 23 | ``` 24 | 25 | [license]: https://img.shields.io/github/license/5pacetoast/pipe.svg 26 | -------------------------------------------------------------------------------- /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /pipe.nim: -------------------------------------------------------------------------------- 1 | import macros 2 | 3 | macro `|>`*(lhs, rhs: untyped): untyped = 4 | case rhs.kind: 5 | of nnkIdent: # single-parameter functions 6 | result = newCall(rhs, lhs) 7 | else: 8 | result = rhs 9 | result.insert(1, lhs) 10 | -------------------------------------------------------------------------------- /pipe.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.1.1" 4 | author = "Chloe Kudryavtsev" 5 | description = "Pipe operator for nim." 6 | license = "Unlicense" 7 | 8 | # Base 9 | 10 | skipDirs = @["tests"] 11 | 12 | task clean, "Clean various files": 13 | rmDir "tests/nimcache" 14 | rmDir "nimcache" 15 | rmfile "pipe" 16 | rmfile "tests/test" 17 | 18 | # Dependencies 19 | 20 | requires "nim >= 0.17.0" 21 | 22 | # Tests 23 | 24 | task test, "Runs the test suite": 25 | exec "nim c -r tests/test" 26 | -------------------------------------------------------------------------------- /tests/nim.cfg: -------------------------------------------------------------------------------- 1 | path=".." 2 | -------------------------------------------------------------------------------- /tests/test.nim: -------------------------------------------------------------------------------- 1 | import pipe 2 | 3 | import unittest 4 | 5 | suite "Pipe operator sanity": 6 | test "Simple calls are equivalent": 7 | proc add2(x: int): int = x + 2 8 | check(add2(2) == 2 |> add2) 9 | 10 | test "Infix operators work": 11 | check(1 + 2 == 1 |> `+` 2) 12 | 13 | test "Chaining works": 14 | check(1 + 2 + 3 == 1 |> `+`(2) |> `+`(3)) 15 | 16 | test "Many parameters work": 17 | proc sum3(x, y, z: int): int = x + y + z 18 | check(sum3(1,2,3) == 1 |> sum3(2,3)) 19 | 20 | test "Varargs work": 21 | proc sum(stuff: varargs[int]): int = 22 | result = 0 23 | for s in stuff: 24 | result += s 25 | 26 | check(sum(1,2,3) == 1 |> sum(2, 3)) 27 | 28 | test "Variables can't be called": 29 | const x = 5 30 | when compiles(5 |> x): fail() 31 | --------------------------------------------------------------------------------