JavaScript How to translate code from Lisp (MIT Schema) into JavaScript using Ramda? translate code from one language to another,translate code from

I'm currently teaching myself functional programming.

I'm trying to translate the following:

(define a 3)
(define b (+ a 1))

(* (cond ((> a b) a)
         ((< a b) b)
         (else -1))
   (+ a 1))

into JavaScript (using Ramda).

One could use nested ternaries, but I like to using the cond function from Ramda. Here is what I did:

const a = 3;
const b = 3 + 1;

cond([[() => a > b, () => a], [() => a < b, () => b], [T, () => -1]])() * (a + 1)

The problem I have with this is that I had to use these functions (e.g. () => 3) instead of just their value (e.g. a).

Is there any way to avoid these functions? Or is there another better way of doing this in JavaScript (maybe even without Rambda)?

I would like to avoid statements such as if, for and switch.

An alternative way to solve this would be using:

import gt from "ramda/src/gt";
import lt from "ramda/src/lt";

const a = () => 3;
const b = () => a() + 1;


cond([[gt(a, b), a], [lt(a, b), b], [T, () => -1]])() * (a() + 1);

Which complicates a and b, since they will always have to be called (see a() + 1).

EDIT:

For some reason the last code where I define a and b as functions doesn't work ????

Answer:1

I think you're working under a bit of a misapprehension. cond is designed to give you a function. It's really not designed for creating a scalar value for an expression. Of course it can do so, simply by immediately calling the function it generates. But that's not the point of it. Moreover, it really is not meant for calling with nullary functions (except in the case of passing T for the default condition.). Again, you can do so, but it's working against the grain of the tool.

Although Ramda takes inspiration from both LISP-style and ML-style functional languages, and although I am personally more a fan of the former, Ramda is closer to the ML world, especially to Haskell. So the main activity it is meant to support is to build functions by composing other functions.

If I were to solve this problem, I probably wouldn't use any Ramda whatsoever, choosing something like this:

const larger = (a, b) => (a > b) ? a : (b > a) ? b : -1
const foo = (a, b) => (a + 1) * larger(a, b)

foo(3, 4) //=> 16
foo(6, 3) //=> 42
foo(3, 3) //=> -4

Or, if I didn't need to reuse larger, I might inline it like this:

const foo = (a, b) => (a + 1) * ((a > b) ? a : (b > a) ? b : -1)

Of course I can write this in Ramda, and in a point-free fashion:

const larger = cond([[gt, unapply(head)], [lt, unapply(last)], [T, always(-1)]])
const foo = converge(multiply, [inc, larger])

Or again, I might inline larger, or replace unapply(head) with nthArg(0) and unapply(last) with nthArg(1).

But none of these options is as readable as the original. Ramda adds nothing here that I can see. Note that I am a big fan of Ramda; I started the library and am one of its principle authors. But I don't think it should be used for every problem.

Answer:2

Using AirBnB's enzyme, we can setState of a component: const loginComponent = shallow(<Login />); loginComponent.setState({ error: true }); I want to do same thing using react-testing-library. ...

Using AirBnB's enzyme, we can setState of a component: const loginComponent = shallow(<Login />); loginComponent.setState({ error: true }); I want to do same thing using react-testing-library. ...

  1. component definition
  2. component to hdmi
  3. component synonym
  4. component form
  5. components of blood
  6. component cable
  7. componentdidupdate
  8. components of fitness
  9. component to hdmi converter
  10. component form of a vector
  11. components of a nucleotide
  12. components of gdp
  13. components of dna
  14. components of physical fitness
  15. component pouch 5e
  16. component speakers
  17. component video
  18. component hardware
  19. components of culture
  20. components of a business plan

I'm using ASP.net 4.5. I have a method like this: protected void OnConfirm(object sender, EventArgs e) { // this function checks if an id exist in database CheckIDExist(123); ...

I'm using ASP.net 4.5. I have a method like this: protected void OnConfirm(object sender, EventArgs e) { // this function checks if an id exist in database CheckIDExist(123); ...

  1. scriptmanager run script after postback
  2. asp.net run script after postback

I have an array: console.log(tenantArray) (8) ["WeWork", "Regus", "Spaces", "Knotel", "RocketSpace", "HQ Global Workspaces", "Other", "Select All"] I also have a large data object, which I want to ...

I have an array: console.log(tenantArray) (8) ["WeWork", "Regus", "Spaces", "Knotel", "RocketSpace", "HQ Global Workspaces", "Other", "Select All"] I also have a large data object, which I want to ...

  1. iterating through array javascript
  2. iterating through array python
  3. iterating through array java
  4. iterating through array c++
  5. iterating through array matlab
  6. iterating through array ruby
  7. iterating through array bash
  8. iterating through array js
  9. iterating through arraylist
  10. iterating through array php
  11. iterating through array swift
  12. iterating through array of objects javascript
  13. iterating through array powershell
  14. iterating through array in typescript
  15. iterating through array in scala
  16. iterating through array in perl

I make one call that retrieves multiple GUIDs using the WebAPI. I loop through each result and update records accordingly. This is all being done asynchronously. I do not want the user to wait for all ...

I make one call that retrieves multiple GUIDs using the WebAPI. I loop through each result and update records accordingly. This is all being done asynchronously. I do not want the user to wait for all ...