fluent syntax using function composition #177
Replies: 8 comments 4 replies
-
Threading would have to be built-in, either through the existing ~> threading module, which would work as is if Racket compatibility remains. I'm dreaming of a language in which the default paradigm is threading. But that's me. |
Beta Was this translation helpful? Give feedback.
-
It's not just you. I even experimented with a macro to make it the default:
;; swaps the first and second datums in an expression, except for ?
(require (prefix-in racket/base/ racket/base) syntax/parse/define)
(define-syntax-parser #%app
[(_ (~literal ?) rest ...) #'(racket/base/#%app rest ...)]
[(_ fn) #'(racket/base/#%app fn)]
[(_ a fn) #'(racket/base/#%app fn a)]
[(_ a fn rest ...) #'(racket/base/#%app fn a rest ...)])
I decided it was better to have an operator, though I think that might have been because the API was built for the status quo...
…On Thu, Sep 23, 2021 at 12:11:47PM -0700, Dexter Santucci wrote:
Threading would have to be built-in, either through the existing ~> threading
module, which would work as is if Racket compatibility remains. I'm dreaming of
a language in which the default paradigm is threading. But that's me.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android. *
|
Beta Was this translation helpful? Give feedback.
-
Thought it might be worth adding that fluent/threading support would help open racket up to the market for quick-and-dirty programming and one-liners. Judging by how many people still use Perl, I'd say this is a pretty big market.
|
Beta Was this translation helpful? Give feedback.
-
Here is my modest contribution to threading macros. I use this in nearly all my projects:
https://github.com/DexterLagan/comp_
Dex
… On Sep 27, 2021, at 4:26 PM, Roger Keays ***@***.***> wrote:
It's not just you. I even experimented with a macro to make it the default:
;; swaps the first and second datums in an expression, except for ?
(require (prefix-in racket/base/ racket/base) syntax/parse/define)
(define-syntax-parser #%app
[(_ (~literal ?) rest ...) #'(racket/base/#%app rest ...)]
[(_ fn) #'(racket/base/#%app fn)]
[(_ a fn) #'(racket/base/#%app fn a)]
[(_ a fn rest ...) #'(racket/base/#%app fn a rest ...)])
I decided it was better to have an operator, though I think that might have been because the API was built for the status quo...
On Thu, Sep 23, 2021 at 12:11:47PM -0700, Dexter Santucci wrote:
> Threading would have to be built-in, either through the existing ~> threading
> module, which would work as is if Racket compatibility remains. I'm dreaming of
> a language in which the default paradigm is threading. But that's me.
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub, or unsubscribe.
> Triage notifications on the go with GitHub Mobile for iOS or Android. *
>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
In F#, I like
As an aside, F# also has |
Beta Was this translation helpful? Give feedback.
-
FYI: the current proposal for Rhombus already supports
(Code snippet taken from #122 (comment)) |
Beta Was this translation helpful? Give feedback.
-
This is beautiful. What about multiple parameters? I’ll check this out though. Thanks!
Dex
… On Sep 28, 2021, at 8:31 AM, sorawee ***@***.***> wrote:
FYI: the current proposal for Rhombus already supports |>. It's not built-in, but you can define it on your own.
#lang rhombus
operator (x |> f): f(x)
fun adder(x):
fun (y):
x + y
fun test():
1 |> adder(10) |> adder(20)
|> adder(30)
test() // outputs 61
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
I also experimented with using dot, but given that racket uses dot for pairs, it wasn't really feasible.
"data.txt" . file->lines . filter (line : line . string-contains? "active") . remove-duplicates . sort
"data.txt".(file->lines).(filter (line : line.(string-contains? "active"))).(remove-duplicates).(sort)
"data.txt".file->lines.filter (line : line.string-contains? "active").remove-duplicates.sort
You could call this "function chaining" instead of "function composition" to win over the object-oriented crowd who are used to method chaining.
Good to see |> is already supported in rhombus. Will | also be a valid operator? AFAICT, racket won't let you do that...
…On Mon, Sep 27, 2021 at 10:56:24PM -0700, plane wrote:
In F#, |> is an infix operator is called "pipe forward" (in F# <| is also
available as "pipe backward").
I like |>:
• It's fairly intuitive, since it looks like an arrow in the direction the
data is flowing.
• It's fairly unique and unambiguous. I can't think of any languages where |>
does something else that would lead to confusion.
As an aside, F# also has >> and << for function composition, again showing the
direction the data is flowing (<< is "backward composition"), which I also
rather like.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android. *
|
Beta Was this translation helpful? Give feedback.
-
One of the first things I do when evaluating new languages is ask myself "does it support, or can I build, a fluent syntax?" By that, I basically mean reading left to right as in UNIX:
cat data.txt | grep "active" | sort | uniq
as opposed to inside out, as in LISP:
(remove-duplicates (sort ((filter (λ (line) (string-contains? line "active")) (file->lines "data.txt")))))
more discussion at https://github.com/rogerkeays/racket-fluent
javascript proposal for the same idea: https://github.com/tc39/proposal-pipeline-operator
Beta Was this translation helpful? Give feedback.
All reactions