[Ramda] Rewrite if..else with Ramda ifElse

From:

const onSeachClick = (searchTerm) => {
   if(searchTerm !== ‘‘) {
      searchForMovies(searchTerm)
   } else {
       console.log(‘a search term should be provided‘)
   }
}

To:

// Utils

const inNotEmpty = R.compose(
   R.not,
   R.isEmpty
);

const onSearchClick = () => {
  R.ifElse(
     isNotEmpty,  // logic to check
     searchForMovices, // do it if true
     log(‘a search term should be provided‘) // do it if false
  )
}    

Example2:

/*
    Example2:
*/
 function processSearchResponse(response) {
     // check total_results prop from response,
     // it shuold greater than 0
     const searchHasResult = R.compose(
         R.lt(0),
         R.prop(‘total_results‘)
     );
     // get results props from response,
     // then createMoviesElements called
     const createElementFromResults = R.compose(
         createMovicesElements,
         R.prop(‘results‘)
     );
     //always return empty
     const createArrayWithNotFound = R.always([
         createMoviceNotFoundElement({})
     ]);

     const elements = R.ifElse(
         searchHasResult,
         createElementFromResults,
         createArrayWithNotFound
     )(response);
 }  
时间: 2024-10-12 15:21:28

[Ramda] Rewrite if..else with Ramda ifElse的相关文章

[Ramda] Change Object Properties with Ramda Lenses

In this lesson we'll learn the basics of using lenses in Ramda and see how they enable you to focus changes on specific properties of an object while keeping your data immutable. what 'R.lens' do is able to get or set prop value but keep the object i

[Ramda] Sort, SortBy, SortWith in Ramda

The difference between sort, sortBy, sortWith is that: 1. sort: take function as args. 2. sortBy: take prop as args. 3. sortWith: take array of funcs as args. const R = require('ramda'); const {sort, sortBy, sortWith, descend, prop, ascend} = R; cons

[Transducer + Ramda] Write highly performance / functional code by using transducer-js and ramda.js libs

Tansducer-js lib A high performance Transducers implementation for JavaScript. Transducers are composable algorithmic transformations. They are independent from the context of their input and output sources and specify only the essence of the transfo

给 JavaScript 开发者讲讲函数式编程

本文译自:Functional Programming for JavaScript People 和大多数人一样,我在几个月前听到了很多关于函数式编程的东西,不过并没有更深入的了解.于我而言,可能只是一个流行词罢了.从那时起,我开始更深地了解函数式编程并且我觉得应该为那些总能听到它但不知道究竟是什么的新人做一点事情. 谈及函数式编程,你可能会想到它们:Haskell 和 Lisp,以及很多关于哪个更好的讨论.尽管它们都是函数式语言,不过的确有很大的不同,可以说各有各的卖点.在文章的结尾处,我希

翻译连载 | 附录 C:函数式编程函数库-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇

原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTML 最坚实的梁柱:分享,是 CSS 里最闪耀的一瞥:总结,是 JavaScript 中最严谨的逻辑.经过捶打磨练,成就了本书的中文版.本书包含了函数式编程之精髓,希望可以帮助大家在学习函数式编程的道路上走的更顺畅.比心. 译者团队(排名不分先后):阿希.blueken.brucecham.cfanlife.d

[Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda&#39;s Converge

When doing comparisons inside of functions, you end of relying heavily on the argument passed into the function. Ramda's converge allows you to do comparisons in a Point-Free style allowing you more flexibility with composing and constructing functio

[Ramda] Filter, Reject and Partition

We'll learn how to get a subset of an array by specifying items to include with filter, or items to exclude using reject. We'll also look at how to get the results from both filter and reject, neatly separated with partition. // we don't need to requ

[React] Update State in React with Ramda&#39;s Evolve

In this lesson we'll take a stateful React component and look at how we can refactor our setState calls to use an updater function and then leverage Ramda's evolvefunction to make our updater function a reusable utility that isn't tied to the React A

[Ramda] Declaratively Map Predicates to Object Properties Using Ramda where

Sometimes you need to filter an array of objects or perform other conditional logic based on a combination of factors. Ramda's where function gives you a concise way to declaratively map individual predicates to object properties, that when combined,