question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add left section, right section and infix functions

See original GitHub issue

See https://github.com/sanctuary-js/sanctuary/pull/391#issuecomment-300962716.

To resolve the issues in #1497 regarding associative, non-commutative binary “operators” I propose adding three new functions inspired by how Haskell deals with infix operators:

// leftSection :: (a, (a -> b -> c)) -> b -> c
// abbreviated _l
const _3subtract = R._l(3, R.subtract);
_3subtract(5); //=> -2

// rightSection :: ((a -> b -> c), b) -> a -> c
// abbreviated _r
const subtract3 = R._r(R.subtract,  3);
subtract3(5); //=> 2

// infix :: (a, (a -> b -> c), b) -> c
// not sure if it needs abbreviating. possibly _in
R.infix(3, R.subtract, 5); //=> -2

Before submitting a PR I’d like to get the semantics locked down. Specifically, should these functions be curried?

My initial thinking is no. These functions are mainly to aid in intent/readability. The examples for the section functions could be rewritten:

const _3subtract = R.subtract(3);
_3subtract(5); //=> -2

const minus3 = R.subtract(R.__, 3); // or R.flip(R.subtract)(3)
minus3(5); //=> 2

This shows that there is no use for curried R._l, and curried R._r is equivalent to R.flip. But the intent is lost.

Without the fully qualified noise:

// 3 subtract 5
_l(3, subtract)(5); //=> -2

// subtract 3 from 5
_r(subtract, 3)(5) //=> 2

// 3 subtract x is less than 5
pipe(_l(3, subtract), _r(lt, 5))( 12 / 3 ); //=> true


The weakest proposal appears to be for infix. After all

infix(1, lt, 2) //=> true

// can just as easily be written
1 < 2 //=> true

But it makes more sense once #1900 is released.

infix([1, 2], lt, [2, 1]) //=> true

@CrossEye I got bored yesterday 😄

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
gabejohnsoncommented, May 19, 2017

@buzzdecafe would you be in favor of removing _isPlaceholder from the curry functions and changing the semantics of __ to the proposed __(__, fn, arg)?

The next best thing IMO is to go with f_, _f, and _f_ or some such while deprecating __.

1reaction
CrossEyecommented, May 4, 2018

I don’t know how it was I never participated in this. This is a great proposal. But I’m torn.

On the one hand, I really like this idea. I’m more in favor of the f_/_f version than the Clojure-inspired __(a, f, b) one. But, while I agree these are better than _l/_r, is there any reason not to simply go with left/right? Perhaps, I suppose, if we think we ever want to support Either directly. There might be some reason for them, then.

On the other hand, this does not solve the problem that lte, etc. will always be surprising in one form or another. It gives us an alternative way to write such things, but it doesn’t remove the surprise. @davidchambers pointed out that the only real solution would be to fully curry our functions. I’m coming more and more to want to do that post-1.0, even if that does change the character of Ramda a fair bit. However, if we do that, how much need would remain for such section functions?

So I’m torn.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Section of an infix operator - HaskellWiki wiki.haskell.org
Essentially, you only give one of the arguments to the infix operator, and it represents a function which intuitively takes an argument and ......
Read more >
3.9. Infix, Prefix and Postfix Expressions - Open Book Project
Let's interpret the troublesome expression A + B * C using operator precedence. B and C are multiplied first, and A is then...
Read more >
Haskell dollar operator and infix operator - Stack Overflow
Short Answer: The expression (3+) is an example of a "section". It is syntactically distinct from usual infix operator syntax (like 2+3 )...
Read more >
Infix Functions In Haskell - The Blog
The infixl means (*) is an infix function, and it is left associative. 7 is the precedence, higher is applied first, on a...
Read more >
4. Operators - Q for Mortals
We examine functions in depth in Chapter 6, but cover some salient points here. ... In q, we can write addition this way...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found