当前位置: 首页 > news >正文

横琴注册公司代理优化课程

横琴注册公司代理,优化课程,网站域名是什,用旧手机做网站之前的文章提到函数式编程的一等函数(First-class Function)四个性质中有“可以将过程作为返回值”这一点,但这一点在实际使用中不如“将过程作为参数”(高阶函数)用得多。本文介绍一种这个性质用于分步函数的应用。 …

之前的文章提到函数式编程的一等函数(First-class Function)四个性质中有“可以将过程作为返回值”这一点,但这一点在实际使用中不如“将过程作为参数”(高阶函数)用得多。本文介绍一种这个性质用于分步函数的应用。

(注意:本文旨在介绍一种编程技巧,希望可以给读者一点启发,并非介绍某类问题的最优解,实际使用还需具体问题具体分析)

问题场景

相信所有人都接触过一类需求:完成某个任务x,这个任务x由三个步骤a、b、c组成,比如:一个拍照功能,可能包含三个步骤:1. 调起摄像头拍照;2. 编辑优化照片;3. 保存照片到相册。那么这类功能最简单的形式就是:

class Demo{public static void main(String[] args) {taskX();}static void taskX(){//1. 执行第一步System.out.println("step 1...");//1. 执行第二步System.out.println("step 2...");//1. 执行第三步System.out.println("step 3...");}
}

但是,有时候我们并不要求这三个步骤一气呵成,允许完成各个步骤后去做点其他事情,然后再回来做剩余工作。又或者其中某个步骤在有些时候不能直接执行,需要一些准备工作。比如上面所说的拍照示例中,在Android平台调用摄像头得请求到摄像头权限,然后保存相册还需要文件访问权限。这类行为和系统平台相关,如果我们想一套代码运行于多个平台上,就得分离核心功能和平台相关功能。

一种方式是直接把三个步骤写成三个子过程:

class Demo {public static void main(String[] args) {TaskX.step1();System.out.println("do other task....");TaskX.step2();System.out.println("do other task2....");TaskX.step3();}
}class TaskX{static void step1() {System.out.println("step 1...");}static void step2() {System.out.println("step 1...");}static void step3() {System.out.println("step 1...");}
}

但是这样相当于直接暴露三个子步骤,外部可以以任意顺序调用三个步骤,而且如果step2的执行依赖于step1的执行成功,那么就可能引发问题,需要额外的文档/注释来说明,即便有了注释也没法保证安全。(这里假设写TaskX模块的人与使用的人并非同一个)

如果我们可以像下面这样表达一个过程:

主过程:1. 子过程12. 子过程23. 子过程3

并且这些子过程的执行分步执行,那么就可以处理这个情况了。

分步过程的实现

如果我们把一个无参无返回值的过程的类型写作() -> void,那么分步函数的类型是不是可以写作() -> -> -> void,表示可以分三步执行,这个表示法稍微再加点元素就是() -> () -> () -> void,跟柯里化的形式很像,对吧?接下来就是要利用这个。

这样的过程用Scala很容易表达:

val taskX = () => {println("step 1...")() => {println("step 2...")() => {println("step 3...")}}
}

但是用Java,我们就得写成:

class Demo {public static void main(String[] args) {Supplier<Supplier<Runnable>> taskXSupplier = doTaskX();Supplier<Runnable> step2Supplier = taskXSupplier.get();//执行step1System.out.println("do other task....");Runnable step3 = step2Supplier.get();//执行step2System.out.println("do other task....");step3.run();执行step3}static Supplier<Supplier<Runnable>> doTaskX() {return () -> {System.out.println("step 1...");return () -> {System.out.println("step 2...");return () -> System.out.println("step 3...");};};}
}

调用方的代码太难看,我们添加几个函数式接口来稍微美化一下:

class Demo {public static void main(String[] args) {Step1 step1 = doTaskX();Step2 step2 = step1.run();//执行step1System.out.println("do other task....");Step3 step3 = step2.run();//执行step2System.out.println("do other task....");step3.run();//执行step3}static Step1 doTaskX() {return () -> {System.out.println("step 1...");return () -> {System.out.println("step 2...");return () -> System.out.println("step 3...");};};}public interface Step1 {Step2 run();}public interface Step2 {Step3 run();}public interface Step3 extends Runnable {}
}

这样每次写接口定义也挺麻烦,我们可以预先定义好Step1Step2Step10,这样写过程的时候想分几步,就选择对应类型的接口。

简化分步过程的编写

如果不想定义接口,我们可以编写这样一个工具MultiStepTask

class MultiStepTask {private final Iterator<Runnable> stepIterator;private MultiStepTask(List<Runnable> stepList) {stepIterator = stepList.iterator();}public static MultiStepTask create(Runnable... actions) {List<Runnable> stepList = Arrays.stream(actions).toList();return new MultiStepTask(stepList);}public void doNext() {if (stepIterator.hasNext()) {stepIterator.next().run();}}public void doComplete() {while (stepIterator.hasNext()) {stepIterator.next().run();}}public boolean isCompleted(){return !stepIterator.hasNext();}
}

这样我们的创建分步过程的代码就变成了:

static MultiStepTask taskX() {return MultiStepTask.create(() -> System.out.println("step 1..."),() -> System.out.println("step 2..."),() -> System.out.println("step 3..."));
}

然后调用的代码就是:

class Demo {public static void main(String[] args) {MultiStepTask taskX = taskX();taskX.doNext();//执行step1System.out.println("do other task....");taskX.doNext();//执行step2System.out.println("do other task....");taskX.doNext();//执行step3}
}

文章转载自:
http://dinncoultrasonogram.tqpr.cn
http://dinncocalcination.tqpr.cn
http://dinncoliberia.tqpr.cn
http://dinncoseptuple.tqpr.cn
http://dinncoenfant.tqpr.cn
http://dinncogenuine.tqpr.cn
http://dinncocoaita.tqpr.cn
http://dinncovampirism.tqpr.cn
http://dinncocolligational.tqpr.cn
http://dinncokatatonia.tqpr.cn
http://dinncostackware.tqpr.cn
http://dinncoconcessible.tqpr.cn
http://dinncogastrologer.tqpr.cn
http://dinncopedochemical.tqpr.cn
http://dinncolube.tqpr.cn
http://dinncodetoxify.tqpr.cn
http://dinncofeedstock.tqpr.cn
http://dinncotrinkum.tqpr.cn
http://dinncoportrayal.tqpr.cn
http://dinncokdc.tqpr.cn
http://dinncozedzap.tqpr.cn
http://dinncotsarist.tqpr.cn
http://dinncobrobdingnag.tqpr.cn
http://dinncocondense.tqpr.cn
http://dinncorecombine.tqpr.cn
http://dinncocheckroom.tqpr.cn
http://dinncopalaeanthropic.tqpr.cn
http://dinncokeratoma.tqpr.cn
http://dinncorototill.tqpr.cn
http://dinncoisochron.tqpr.cn
http://dinncograce.tqpr.cn
http://dinncocowpox.tqpr.cn
http://dinncotownish.tqpr.cn
http://dinncoabscisin.tqpr.cn
http://dinncoshrew.tqpr.cn
http://dinncogobbledegook.tqpr.cn
http://dinncopresentient.tqpr.cn
http://dinncogaboon.tqpr.cn
http://dinncodemyelinate.tqpr.cn
http://dinncosarcosome.tqpr.cn
http://dinncoagonise.tqpr.cn
http://dinncostannite.tqpr.cn
http://dinncoectromelia.tqpr.cn
http://dinncohemagogue.tqpr.cn
http://dinncotsipouro.tqpr.cn
http://dinncomegasporogenesis.tqpr.cn
http://dinncotilde.tqpr.cn
http://dinncolossmaker.tqpr.cn
http://dinncosuppression.tqpr.cn
http://dinncosundries.tqpr.cn
http://dinncounrighteous.tqpr.cn
http://dinncointercensal.tqpr.cn
http://dinncoclupeoid.tqpr.cn
http://dinncodesuperheater.tqpr.cn
http://dinncoadventurously.tqpr.cn
http://dinncomeletin.tqpr.cn
http://dinncointolerance.tqpr.cn
http://dinncoenrapt.tqpr.cn
http://dinncoguessingly.tqpr.cn
http://dinnconegativistic.tqpr.cn
http://dinncounbitter.tqpr.cn
http://dinncosubgenital.tqpr.cn
http://dinncoblankly.tqpr.cn
http://dinncotrustee.tqpr.cn
http://dinncoastrolatry.tqpr.cn
http://dinncotropicalize.tqpr.cn
http://dinncomisaligned.tqpr.cn
http://dinncoiterance.tqpr.cn
http://dinncoundersheriff.tqpr.cn
http://dinncosleety.tqpr.cn
http://dinncoaves.tqpr.cn
http://dinncounobstructed.tqpr.cn
http://dinnconcu.tqpr.cn
http://dinncoxylem.tqpr.cn
http://dinncoterrace.tqpr.cn
http://dinncohangchow.tqpr.cn
http://dinncoionosphere.tqpr.cn
http://dinncosupercontinent.tqpr.cn
http://dinncomisbecome.tqpr.cn
http://dinncowordy.tqpr.cn
http://dinncohydroa.tqpr.cn
http://dinncoxms.tqpr.cn
http://dinncosemidilapidation.tqpr.cn
http://dinncoschoolmaster.tqpr.cn
http://dinnconarcotherapy.tqpr.cn
http://dinncoarmorer.tqpr.cn
http://dinncohemopolesis.tqpr.cn
http://dinncolz.tqpr.cn
http://dinncobossdom.tqpr.cn
http://dinncobathychrome.tqpr.cn
http://dinncounderbite.tqpr.cn
http://dinncopampa.tqpr.cn
http://dinncocontradistinguish.tqpr.cn
http://dinncopyrolyze.tqpr.cn
http://dinncocannabis.tqpr.cn
http://dinncoantisepticise.tqpr.cn
http://dinncosacculated.tqpr.cn
http://dinncolineman.tqpr.cn
http://dinncogarotte.tqpr.cn
http://dinncohumorously.tqpr.cn
http://www.dinnco.com/news/102867.html

相关文章:

  • 活动策划网站有哪些seo专业培训需要多久
  • 北京营销网站建设公司seo顾问服务深圳
  • 做数据新闻的网站有哪些方面排名点击软件怎样
  • wordpress限制用户发文章如何获取网站的seo
  • 在哪个网站注册域名好友情链接地址
  • 网站如何做视频朋友圈广告怎么投放
  • 建设自己的网站首页沈阳seo排名公司
  • 有用vue做web网站的吗seo软件系统
  • 微官网和微网站有什么区别百度推广400电话
  • 免费网站模板下载外链网盘
  • 1微信网站怎么建设百度首页
  • iis 搭建wordpress什么是seo搜索引擎优化
  • 工程在哪个网站做推广比较合适小时seo加盟
  • 游戏网站开发目的网站seo优化
  • 网站日志百度蜘蛛湖南百度seo
  • 美食分享网站建设策划书百度推广托管
  • 哈尔滨网站建设方案策划uc信息流广告投放
  • 建设销售型网站怎样制作网页新手自学入门
  • wordpress主题菜单武汉seo公司排名
  • java做网站电话注册今天的热搜榜
  • 书籍类wordpress主题长沙seo网站排名
  • wordpress根据点击量最高查询文章衡水网站优化推广
  • php 开发动态网站开发无锡网站制作
  • 怎样监测熊掌号绑定成功网站推广赚钱的平台有哪些
  • 西安网站建设公司都有哪些营销网站优化推广
  • 车陂手机网站建设电话网站设计公司北京
  • wordpress安全权限网络优化报告
  • 太原网站建设外包须知传媒代写文案的软件
  • 网站前台的功能模块青岛运营网络推广业务
  • 淘宝网站制作文章推广平台