ramda.js

import R (ramda) into typescript .ts file

泪湿孤枕 提交于 2019-12-22 08:47:53
问题 I am attempting to use Ramda.js as follows: /// <reference path="../../../node_modules/@types/ramda/index.d.ts" /> module App { var settab = R.once((element) => current(element)); function current(li: any) { // ... } } I get error, Cannot find name 'R' In the case of the ramda/index.d.ts file, the declaration (with detail omitted) is as follows: declare var R: R.Static; declare namespace R { type Ord = number | string | boolean; interface Static { // ......... } } export = R; 回答1: You have to

How to use Ramda to find matching object in Array by key value

人走茶凉 提交于 2019-12-22 06:09:10
问题 Ramda REPL example var portfolio = [{ticker: "aa"}, {ticker: "bb"}]; var ticker = {ticker:"aa"}; var exist = R.find(R.propEq('ticker', ticker), portfolio) console.log(exist) Currently this is giving me undefined , however R.propEq should find the matching object by key ticker in port I thought? 回答1: As you say, you can solve it by passing in the key to propEq: R.find(R.propEq('ticker', 'aa'), port) Another option is to use the eqProps function, which tests if two objects match for the named

Ramda: Is there a way to 'fork' a parameter to two functions during pipe?

心不动则不痛 提交于 2019-12-21 20:49:33
问题 I'm a functional programming beginner. I'm working on a React Native app using Ramda. The app lets users maintain their houses. I have written function called asyncPipe which lets me pipe promises and normal functions. I use it for the loginFlow which currently has a http request ( getHouseList ) as its last function. const asyncPipe = (...fns) => x => fns.reduce(async (y, f) => f(await y), x); const loginFlow = asyncPipe( // ... someFunctions getHouseList ); // used later like this in

Using Ramda, and pointfree style, how can I copy the first item of an array to the end of it?

淺唱寂寞╮ 提交于 2019-12-20 04:38:18
问题 I want to take an array [1, 2, 3] and return [1, 2, 3, 1] . I'm using Ramda, and I can get the desired result like this: const fn = arr => R.append(R.prop(0, arr), arr); But I'd like to do it point-free. Here's the closest I've gotten: const fn = R.compose(R.append, R.prop(0)); fn(arr)(arr) But that looks silly. What am I missing? Thanks! 回答1: converge can be very helpful for things like this. const rotate = R.converge(R.append, [R.head, R.identity]) rotate([1, 2, 3]); //=> [1, 2, 3, 1] 回答2:

Where can I find an explanation/summary of symbols used to explain functional programming, specifically Ramda.js?

我的未来我决定 提交于 2019-12-17 19:46:29
问题 The API documentation for the JavaScript functional programming library Ramda.js contains symbolic abbreviations but does not provide a legend for understanding these. Is there a place (website, article, cheatsheet, etc.) that I can go to to decipher these? Some examples from the Ramda.js API documentation: Number -> Number -> Number Apply f => f (a -> b) -> f a -> f b Number -> [a] -> [[a]] (*... -> a) -> [*] -> a {k: ((a, b, ..., m) -> v)} -> ((a, b, ..., m) -> {k: v}) Filterable f => (a ->

How do I return a component with passed props with R.ifElse?

梦想与她 提交于 2019-12-13 08:41:18
问题 I'm diving into ramdajs and refactoring some react code here. It might not necessary to do it in this case but for the sake of exercise how do I pass props in R.ifElse ? {!this.state.repos ? <Loader /> : <RepoGrid repos={this.state.repos} />} // refactoring to: {R.ifElse( R.isEmpty, R.always(Loader), RepoGrid )(this.state.repos)} This gives me an error Cannot read property '@@transducer/step' of null const ReposGrid = R.pipe( R.tap(console.log) R.prop("repos"), R.map(Repo), ReposWraper ) 回答1:

How to use Ramda pipe?

守給你的承諾、 提交于 2019-12-13 03:59:30
问题 Background I am trying to compose 2 functions using Ramda, but I am having issue with pipe , meaning I don't know how to use it. Code Let's imagine I have a function that returns an array: var createQuery = params => [ getSQLQuery( params ), [ getMarket() ] ]; var getSQLQuery = ( { lang } ) => `My query is ${lang}`; var getMarket = () => "en_en" So, when calling createQuery({ lang: "es" }) I would have the following output: [ "My query is es", ["en_en"] ] ; Now, let's also assume I am a nasty

Take top X total items in a round robin from multiple arrays with Ramda

拜拜、爱过 提交于 2019-12-12 19:24:42
问题 I have an array of arrays and want to write a function that returns the top x number of items, by taking items from each array in order. Here is an example of what I'm after: const input = [ ["1a", "2a", "3a", "4a", "5a"], ["1b", "2b", "3b", "4b", "5b"], ["1c", "2c", "3c", "4c", "5c"], ["1d", "2d", "3d", "4d", "5d"] ]; const takeRoundRobin = count => arr => { // implementation here }; const actual = takeRoundRobin(5)(input); const expected = [ "1a", "1b", "1c", "1d", "2a" ]; I saw a

How can I access iteration index in Ramda.map

。_饼干妹妹 提交于 2019-12-12 08:17:48
问题 I used to write something like _.map(items, (item, index) => {}); with lodash. Usually I don't need index but sometimes it's useful. I'm migrating to Ramda now: R.map((item, index) => {}, items); index is undefined . Sure, I can create variable index in upper scope and increment it every time in map body but it's kinda wrong from FP point of view that Ramda stands for. So is there's any build in way of getting iteration index? 回答1: Check out addIndex: Creates a new list iteration function

TypeScript rejection with `Type '{}[]' is not assignable to type 'number'`

橙三吉。 提交于 2019-12-11 18:42:10
问题 Let's say I create a simple function to double its input, > let f1: (x: number) => number = x => x * 2; > .type f1 let f1: (x: number) => number If I want to take the first value and double it, I can do either let f2 = R.pipe( R.take(1), f1 ); let f2 = R.pipe( R.head, f1 ); Both of these work f2([5,4,3]) outside of TypeScript. However, with TypeScript I get, > let f2 = R.pipe( R.take(1), f1 ); [eval].ts(6,29): error TS2345: Argument of type '(x: number) => number' is not assignable to