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.

curry modifies functions returned from constant

See original GitHub issue

Describe the bug When a constant function is used to return another function, the returned function is automatically curried. This results in the return value no longer matching what was given to constant.

Based on my rough digging, it appears related to the logic of how the curry function works, specifically lines 25-27 in src/core/curry.js. This checks if the value returned by applying the arguments is a function, if it is, it curries the function.

This causes

To Reproduce

import { constant } from 'crocks';

// This is not curried, and should be returned unmodified.
const identity = x => x;

const constantOfIdentity = constant(identity);

// Instead, we see that the returned version is now the definition of the curry function.
console.log(constantOfIdentity().toString());

// This returns false, these are no longer reference equivalent.
console.log(constantOfIdentity() === identity);

Expected behavior The original value given to constant should be returned unmodified.

Additional context This affects any autocurried crocks function that can wind up returning a function itself. I’ve seen the same behavior with ifElse and when.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
evilsoftcommented, Dec 22, 2018

@goulderb yeah, I was afraid you were going to say tcomb or something to do with specs/tests. It was because of tcomb, that we started adding things like isSameType and a lot of the type predicates. I would recommend doing what you have been for now. We have been thinking of ways we can answer this issue of equality vs equivalence and do not have an answer yet.

But as soon as we do, we will be sure to broadcast it to the world.

0reactions
dalefrancis88commented, Feb 14, 2019

Closing this for now as it seems resolved. Feel free to re-open if there is more to discuss

Read more comments on GitHub >

github_iconTop Results From Across the Web

Everything about Currying in JavaScript | by Chidume Nnamdi
It keeps returning a new function (that expects the current argument, like we said earlier) until all the arguments are exhausted. The arguments ......
Read more >
Closures, Curried Functions, and Cool Abstractions in ...
In this article, we will talk about closures and curried functions and we'll play around with these concepts to build cool abstractions.
Read more >
TypeScript and currying - gists · GitHub
Note that the curried function has any as its return type. This is to make the compiler relax, which would otherwise have to...
Read more >
Javascript curry: context in returned function - Stack Overflow
In a curry function like this : var curry = function() { var slice = Array.prototype.slice ...
Read more >
Javascript Currying - Variadic Currying - Refine Dev
We defined two versions of the curry() function. The first one maintains unarity of the accumulator function at every level of the recursive ......
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