bash-fun Functional programming in bash开源项目

我要开发同款
匿名用户2021年11月09日
66阅读
开发技术SHELL
所属分类终端/远程登录、应用工具
授权协议MIT License

作品详情

Introduction

Introductiontofun.shlibrary

Quickstart#!/bin/bash.<(test-efun.sh||curl-Lshttps://raw.githubusercontent.com/ssledz/bash-fun/master/src/fun.sh>fun.sh;catfun.sh)seq14|sumFunctionsoverviewappendbuffcallcatchcurrydivdropdropwfactorialfilterfoldlfoldrisintisemptyisfileisnonzerofileisreadableiswritableisdirjoinlambdalastlheadlistltaillzipmapmaybemaybemapmaybevaluemodmulnotntupntuplntuprntupxpeekplusprependproductretresreversrevers_strscanlsplitcstripstriplstriprsubsumtaketrytuptupltuprtupxunlistλwith_trampolinelist/unlist$list123123$list12345|unlist12345take/drop/ltail/lhead/last$list1234|drop234$list12345|lhead1$list1234|ltail234$list12345|last5$list12345|take212join$list12345|join,1,2,3,4,5$list12345|join,[][1,2,3,4,5]map$seq15|mapλa.'echo$((a+5))'678910$listabsde|mapλa.'echo$a$(echo$a|tra-zA-Z)'aAbBsSdDeE$list123|mapecho123$list123|map'echo$isanumber'1isanumber2isanumber3isanumber$list1234|map'echo\($,$\)isapoint'(1,1)isapoint(2,2)isapoint(3,3)isapoint(4,4)isapointflatmap$seq23|mapλa.'seq1$a'|join,[][1,2,1,2,3]$listabc|mapλa.'echo$a;echo$a|tra-zA-z'|join,[][a,A,b,B,c,C]filter$seq110|filterλa.'[[$(mod$a2)-eq0]]&&rettrue||retfalse'246810foldl/foldr$listabcd|foldlλaccel.'echo-n$acc-$el'a-b-c-d$list''abcd|foldrλaccel.\'if[[!-z$acc]];thenecho-n$acc-$el;elseecho-n$el;fi'd-c-b-a$seq14|foldlλaccel.'echo$(($acc+$el))'10$seq14|foldlλaccel.'echo$(mul$(($acc+1))$el)'64#1+(1+1)*2+(4+1)*3+(15+1)*4=64$seq14|foldrλaccel.'echo$(mul$(($acc+1))$el)'56#1+(1+1)*4+(8+1)*3+(27+1)*2=56tup/tupx/tupl/tupr$tupa1(a,1)$tup'foobar'1'one'2(foobar,1,one,2)$tup,13(u002c,1,3)$tupl$(tupa1)a$tupr$(tupa1)1$tup,13|tupl,$tup'foobar'1'one'2|tuplfoobar$tup'foobar'1'one'2|tupr2$tup'foobar'1'one'2|tupx21$tup'foobar'1'one'2|tupx1,3foobarone$tup'foobar'1'one'2|tupx2-41one2ntup/ntupx/ntupl/ntupr$ntuptuplesthat$(ntupsafelynest)(dHVwbGVzCg==,dGhhdAo=,KGMyRm1aV3g1Q2c9PSxibVZ6ZEFvPSkK)echo'(dHVwbGVzCg==,dGhhdAo=,KGMyRm1aV3g1Q2c9PSxibVZ6ZEFvPSkK)'|ntupx3|ntuprnest$ntup'foo,bar'1one1(Zm9vLGJhcgo=,MQo=,b25lCg==,MQo=)$echo'(Zm9vLGJhcgo=,MQo=,b25lCg==,MQo=)'|ntupx1foo,bar$ntupl$(ntup'foobar'1one2)foobar$ntupr$(ntup'foobar'1one2)2buff$seq110|buffλab.'echo$(($a+$b))'37111519$seq110|buffλabcde.'echo$(($a+$b+$c+$d+$e))'1540lzip$listabcdef|lzip$(seq110)(a,1)(b,2)(c,3)(d,4)(e,5)(f,6)$listabcdef|lzip$(seq110)|last|tupr6curryadd2(){echo$(($1+$2))}$curryincadd21$inc23$seq13|mapλa.'inc$a'234peek$list123\|peeklambdaa.echo'dbga:$a'\|maplambdaa.'mul$a2'\|peeklambdaa.echo'dbgb:$a'\|sumdbga:1dbga:2dbga:3dbgb:2dbgb:4dbgb:612$a=$(seq14|peeklambdaa.echo'dbg:$a'|sum)dbg:1dbg:2dbg:3dbg:4$echo$a10maybe/maybemap/maybevalue$listHello|maybe(Just,Hello)$list""|maybe(Nothing)$listHello|maybe|maybemapλa.'troHOh<<<$a'(Just,hellO)$list""|maybe|maybemapλa.'troHOh<<<$a'(Nothing)$echobash-funrocks|maybe|maybevalueDEFAULTbash-funrocks$echo|maybe|maybevalueDEFAULTDEFAULTnot/isint/isempty$isint42true$listblah|isintfalse$nottruefalse$notisint777false$list12""cd6|filterλa.'isint$a'126$list12""cd6|filterλa.'notisempty$a'12cd6isfile/isnonzerofile/isreadable/iswritable/isdir$touch/tmp/foo$isfile/tmp/footrue$notiswritable/true$files="/etc/passwd/etc/sudoers/tmp/tmp/foo/no_such_file"$list$files|filterλa.'isfile$a'/etc/passwd/etc/sudoers/tmp/foo$list$files|filterλa.'isdir$a'/tmp$list$files|filterλa.'isreadable$a'/etc/passwd/tmp/tmp/foo$list$files|filterλa.'iswritable$a'/tmp/tmp/foo$list$files|filterλa.'isnonzerofile$a'/etc/passwd/etc/sudoers/tmp$list$files|filterλa.'notisfile$a'/tmp/no_such_filetry/catch$echo'expr2/0'|tryλ_.'echo0'0$echo'expr2/0'|tryλstatus.'echo$status'2$echo'expr2/2'|tryλ_.'echo0'1tryλ_.'echosomeerrorsduringpull;exit1'<<(echogitpull)$echo'expr2/0'\|LANG=encatchλcmdstatusval.'echocmd=$cmd,status=$status,val=$val'cmd=expr2/0,status=2,val=(expr:,division,by,zero)$echo'expr2/2'|catchλ__val.'tupl$val'1scanl$seq15|scanllambdaaccel.'echo$(($acc+$el))'1361015$seq15|scanllambdaab.'echo$(($a+$b))'|last15with_trampoline/res/callfactorial(){fact_iter(){localproduct=$1localcounter=$2localmax_count=$3if[[$counter-gt$max_count]];thenres$productelsecallfact_iter$(echo$counter\*$product|bc)$(($counter+1))$max_countfi}with_trampolinefact_iter11$1}$timefactorial30|fold-w70265252859812191058636308480000000real0m1.854suser0m0.072ssys0m0.368stimefactorial60|fold-w708320987112741390144276341183223364380754172606361245952449277696409600000000000000real0m3.635suser0m0.148ssys0m0.692s$timefactorial90|fold-w701485715964481761497309522733620825737885569961284688766942216863704985393094065876545992131370884059645617234469978112000000000000000000000real0m4.371suser0m0.108ssys0m0.436sExamplesprocessNames(){uppercase(){localstr=$1echo$(tr'a-z''A-Z'<<<${str:0:1})${str:1}}list$@\|filterλname.'[[${#name}-gt1]]&&rettrue||retfalse'\|mapλname.'uppercase$name'\|foldlλaccel.'echo$acc,$el'}processNamesadammonikasslawekddanielBartekjkAdam,Monika,Slawek,Daniel,BartekRunningtestscdtest./test_runnerContributionguidelines

Feelfreetoaskquestionsinchat,openissues,orcontributebycreatingpullrequests.

Inordertocreateapullrequest

checkoutmasterbranchintroduceyourchanges&bumpversionsubmitpullrequestResourcesInspirationFunctionalProgramminginBash
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论