haskell

Does Haskell provide an idiom for pattern matching against many possible data constructors?

余生颓废 提交于 2020-05-08 14:28:05
问题 Working on a Haskell project, I'm dealing with the Event data type from the FSNotify package. The constructors for Event are all: Added FilePath UTCTime Modified FilePath UTCTime Removed FilePath UTCTime In my code, I'm only interested in extracting the FilePath from the Event and doing the same action regardless of the type constructor; because of this, I'm tempted to make a lambda. Unfortunately, the code suffers reduced readability when I drop a case expression into the lambda to pattern

Does Haskell provide an idiom for pattern matching against many possible data constructors?

跟風遠走 提交于 2020-05-08 14:28:01
问题 Working on a Haskell project, I'm dealing with the Event data type from the FSNotify package. The constructors for Event are all: Added FilePath UTCTime Modified FilePath UTCTime Removed FilePath UTCTime In my code, I'm only interested in extracting the FilePath from the Event and doing the same action regardless of the type constructor; because of this, I'm tempted to make a lambda. Unfortunately, the code suffers reduced readability when I drop a case expression into the lambda to pattern

小甲鱼Python第十一讲课后习题

倖福魔咒の 提交于 2020-05-05 21:50:26
0. 注意,这道题跟上节课的那道题有点儿不同,回答完请上机实验或参考答案。 old = [1, 2, 3, 4, 5] new = old old = [6] print(new) 如果不上机操作,你觉得会打印什么内容? 1,2,3,4,5 1. 请问如何将下边这个列表的'小甲鱼'修改为'小鱿鱼'? list1 = [1, [1, 2, ['小甲鱼']], 3, 5, 8, 13, 18] list1[1][2]=['小鱿鱼'] 2. 要对一个列表进行顺序排序,请问使用什么方法? 列表名.sort() 3. 要对一个列表进行逆序排序,请问使用什么方法? 列表名.sort() 列表名.reverse() 或 列表名.sort(reverse=True) 4. 列表还有两个内置方法没给大家介绍,不过聪明的你应该可以自己摸索使用的门道吧:copy() 和 clear() list1=[1,2,3,4] list1.clear() #clear()不添加参数 print(list1)----[] list2=list1.copy() print(list2)-----[1,2,3,4] 5. 你有听说过列表推导式或列表解析吗? 列表推导式(List comprehensions)也叫列表解析,灵感取自函数式编程语言 Haskell。Ta 是一个非常有用和灵活的工具,可以用来动态的创建列表

2020年2月中国编程语言排行榜

瘦欲@ 提交于 2020-05-03 19:26:45
编程语言比例 排名 编程语言 平均工资 工资中位数 最低工资 最高工资 人头 人头百分比 1 rust 21433 20000 5266 45000 369 0.11% 2 typescript 18727 22500 6500 30000 1841 0.57% 3 go 18292 16000 6175 40000 23860 7.35% 4 lua 18219 17500 5000 37500 2676 0.82% 5 python 17834 15000 3750 45000 26507 8.17% 6 haskell 17713 15714 7500 27000 47 0.01% 7 ruby 17420 17500 3750 37500 913 0.28% 8 matlab 17384 16000 5000 37500 5030 1.55% 9 julia 16525 12500 12500 25000 20 0.01% 10 perl 16326 14583 4950 37500 2102 0.65% 11 kotlin 16318 15000 6500 34521 803 0.25% 12 cpp 15763 13500 3750 37500 55817 17.20% 13 swift 15657 13500 5250 36225 2236 0.69% 14

Vavr Option:Java Optional 的另一个选项

房东的猫 提交于 2020-05-01 17:41:33
每当涉及Java,总会有很多选项。 这篇文章讨论了 Java 基础类 Optional 用法,与 Vavr 中的对应方法进行比较。Java 8最早引入了 Optional,把它定义为“一种容器对象,可以存储 null 或非 null 值”。 通常,在返回值可能为null的地方,会出现NullPointerException。开发人员可以使用 Optional 避免 null 值检查。在这种情况下,Optional 提供了一些方便的功能。但可惜的是,Java 8并没有包含所有功能。Optional中的某些功能需要使用 Java 11。要解决这类问题还可以使用 Vavr Option类。 本文将介绍如何使用 Java Optional类,并与 Vavr Option 进行比较。注意:示例代码要求使用Java 11及更高版本。所有代码在 Vavr0.10.2环境下完成测试。 让我们开始吧。 Java Optional 简介 Optional 并不是什么新概念,像 Haskell、Scala 这样的函数式编程语言已经提供了实现。调用方法后,返回值未知或者不存在(比如 null)的情况下,用 Optional 处理非常好用。下面通过实例进行介绍。 新建 Optional 实例 首先,需要获得 Optional 实例,有以下几种方法可以新建 Optional 实例。不仅如此

The lower value from a tuple of words and values in haskell

不想你离开。 提交于 2020-05-01 06:39:55
问题 I'm trying to write a function that chooses the tuple with the lower value and the tuple is formed by a word and a value. For example, for a list of tuples like this one: [("CHURCH",262),("PENCIL",311),("FLOUR",175),("FOREST",405),("CLASS",105)], the function would return ("CLASS",105) Can someone help me out? :) Thank you so much! 回答1: Try this: foldl1 (\acc x -> if snd x < snd acc then x else acc) <your list> You fold your list by comparing the current element of the list to the next one.

The lower value from a tuple of words and values in haskell

可紊 提交于 2020-05-01 06:35:20
问题 I'm trying to write a function that chooses the tuple with the lower value and the tuple is formed by a word and a value. For example, for a list of tuples like this one: [("CHURCH",262),("PENCIL",311),("FLOUR",175),("FOREST",405),("CLASS",105)], the function would return ("CLASS",105) Can someone help me out? :) Thank you so much! 回答1: Try this: foldl1 (\acc x -> if snd x < snd acc then x else acc) <your list> You fold your list by comparing the current element of the list to the next one.

Java8 lambda表达式10个示例<转>

独自空忆成欢 提交于 2020-04-28 09:03:05
例1、用lambda表达式实现Runnable 我开始使用Java 8时,首先做的就是使用lambda表达式替换匿名类,而实现Runnable接口是匿名类的最好示例。看一下Java 8之前的runnable实现方法,需要4行代码,而使用lambda表达式只需要一行代码。我们在这里做了什么呢?那就是用() -> {}代码块替代了整个 匿名类 。 // Java 8之前: new Thread(new Runnable() { @Override public void run() { System.out.println("Before Java8, too much code for too little to do"); } }).start(); //Java 8方式: new Thread( () -> System.out.println("In Java8, Lambda expression rocks !!") ).start(); 输出: too much code, for too little to do Lambda expression rocks !! 这个例子向我们展示了Java 8 lambda表达式的语法。你可以使用lambda写出如下代码: (params) -> expression (params) -> statement (params)

[原创]使用 Pandoc 实现 Markdown 文件转 PDF 文件

一世执手 提交于 2020-04-25 09:45:26
Markdown 写法简单明快,我十分喜欢,以至于我最近都想使用 Markdown 快速的进行测试说明书的写作,但是考虑到这文档是要进行交接的,一个 .md 的文件在内部传输还是有不便,于是就想到了能不能把 Markdown 转为 PDF 文件作为通用文件。 然后搜索后看到了这样的一篇文章 如何把 Markdown 文件转化为 PDF ,文中提到了很多方法,有使用 .md -->> .html -->> .pdf 的,也有 .md -->> .word -->> .pdf 的,也有 .md -->> .tex -->>.pdf 的。最终考虑到之前曾经使用 LaTex 进行过文章写作,并且 PC 中还有着 LaTex 环境,决定 使用 Pandoc 实现 Markdown 转 PDF 。 <!--more--> Pandoc 介绍 Pandoc 是一个用 haskell 编写的开源文本转换工具,小巧迅速且支持格式广泛,堪称文本转换应用的瑞士军刀。支持很多种输入输出,有关 Pandoc 可以在其 官网 进行详细了解。下载页面可以 点此进入 ,在其中选择合适的版本即可(GitHub 下载不多赘述)。 Markdown 转 PDF 全英文文档转换 在需要转换的文件路径下进行 Shift+鼠标右键 选择此处进入命令行,键入 pandoc input.md -o output.pdf

Scala的柯里化及其应用

无人久伴 提交于 2020-04-23 21:53:28
一、概念 柯里化(currying, 以逻辑学家Haskell Brooks Curry的名字命名)指的是将原来接受两个参数的函数变成新的接受一个参数的函数的过程。新的函数返回一个以原有第二个参数作为参数的函数。 在Scala中方法和函数有细微的差别,通常编译器会自动完成方法到函数的转换。 二、Scala中柯里化的形式 Scala中柯里化方法的定义形式和普通方法类似,区别在于柯里化方法拥有多组参数列表,每组参数用圆括号括起来,例如: mysum方法拥有两组参数,分别是(x: Int)和(y: Int)。 mysum方法对应的柯里化函数类型是: Int => Int = >Int 柯里化函数的类型声明是右结合的,即上面的类型等价于: Int => (Int = >Int) 表明该函数若只接受一个Int参数,则返回一个Int => Int类型的函数,这也和柯里化的过程相吻合。 三、示例 上面的代码定义了一个柯里化方法,在Scala中可以直接操纵函数,但是不能直接操纵方法,所以在使用柯里化方法前,需要将其转换成柯里化函数。最简单的方式是使用编译器提供的语法糖: 使用Scala中的部分应用函数(partially applied function)技巧也可以实现转换,但是请注意转后后得到的并不是柯里化函数,而是一个接受两个(而不是两组)参数的普通函数 传入一个参数