JavaScript中的函数与命令模式,Java也有参考价值

栏目: JavaScript · 发布时间: 6年前

内容简介:命令范式:函数范式:if语句可以被函数getGreeting替代,从而重用:
这篇文章的目的并不是说一种范式比另一种范式更好。它只是显示您遇到的常见模式及其同等的函数实现方法。(banq注:其实对使用Java Stream替代if/else也有参考作用) if / else

命令范式:

const hour = 14
let greeting

if (hour < 18) {
  greeting = 'Good day';
} else {
  greeting = 'Good evening';
}

函数范式:

if语句可以被函数getGreeting替代,从而重用:

const isDay = hour => hour < 18
const getGreeting = hour => isDay(hour) ? 'Good Day' : 'Good Evening'
const greeting = getGreeting (hour)

if (无else)

命令范式:

let name = 'joel'

if (name != null) {
  name = name.toUpperCase()
}

函数范式:
isNotnull和toUpperCase市可重用的函数:
import when from 'mojiscript/logic/when'

const isNotNull = obj => obj != null
const toUpperCase = when (isNotNull) (string => string.toUpperCase ())

const name = 'joel'
const upperName = toUpperCase (name)


[b]数组相加[/b]

命令范式:

const values = [1, 2, 3]

let sum = 0
for (const x of values) {
  sum = sum + x
}

函数范式:
不将sum变成可变变量:
const values = [1, 2, 3]

const add = (x, y) => x + y
const sum = values.reduce(add)
[b]for/if[/b]
命令范式:
const values = [1, 2, 3, 4, 5]

let evens = []
for (const x of values) {
  if (x % 2 === 0) {
    evens.push(x)
  }
}

函数范式:
不要将evens成为可变:

import filter from 'mojiscript/list/filter'

const values = [1, 2, 3, 4, 5]

const isEven = num => num % 2 === 0
const evens = filter (isEven) (values)


[b]提前中断循环[/b]
命令范式:
const values = [1, 2, 3]
let sum = 0
for (const x of values) {
  if (x > 3) break
  sum = sum + x 
}
函数范式:
使用reduceWhile,类似reduce,能接受一个

predicate提早结束。

import reduceWhile from 'mojiscript/list/reduceWhile'

const add = x => y => x + y
const lte3 = num => num <= 3

const sum = reduceWhile (() => lte3) (add) (0) (values)

if / else if / else

命令范式:
const fn = temp => {
   if (temp === 0) return 'water freezes at 0°C'
   else if (temp === 100) return 'water boils at 100°C'
   else return `nothing special happens at ${temp}°C`
}

fn(0) //=> 'water freezes at 0°C'
fn(50) //=> 'nothing special happens at 50°C'
fn(100) //=> 'water boils at 100°C'

函数范式:
import cond from 'mojiscript/logic/cond'
import $ from 'mojiscript/string/template'

const fn = cond([
  [0, 'water freezes at 0°C'],
  [100, 'water boils at 100°C'],
  [() => true, $`nothing special happens at ${0}°C`]
])

fn(0) //=> 'water freezes at 0°C'
fn(50) //=> 'nothing special happens at 50°C'
fn(100) //=> 'water boils at 100°C'

[b]类
[/b]命令范式:
class Cat {
  constructor() {
    this.sound = 'Meow'
  }

  talk() {
    return this.sound
  }
}

const cat = new Cat()
const talk = cat.talk

cat.talk() //=> 'Meow'
talk() //=> Error: Cannot read property 'sound' of undefined

函数范式:
const cat = {
  sound: 'Meow'
}

const dog = {
  sound: 'Woof'
}

const talk = animal => animal.sound

talk (cat) //=> 'Meow'
talk (dog) //=> 'Woof'
检查空NULL

命令范式:
const toUpper = string => {
  if (string != null) {
    return string.toUpperCase()
  }
}

函数范式:

此示例将参数包装在一个

Maybe

类型中,然后在最后将其解包。在一般FP应用程序中,可在整个应用程序使用

Maybe

,不需要打包和解包

string

。所以这比你通常看到的要冗长一些。

import S from 'sanctuary'

const toUpper = S.pipe ([

S.toMaybe,

S.map (string => string.toUpperCase ()),

S.maybeToNullable

])

// If you use `Maybe` throughout your app, this would be your `toUpper` function.

const toUpper = S.map (string => string.toUpperCase ());


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数学拾遗

数学拾遗

加黑蒂 / 清华大学出版社 / 2004-8 / 49.00元

Beginning graduate students in mathematics and other quantitative subjects are expected to have a daunting breadth of mathematical knowledge ,but few have such a backgroud .This book will help stedent......一起来看看 《数学拾遗》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具