haskell

科技爱好者周刊:第 103 期

走远了吗. 提交于 2020-04-21 23:36:16
这里记录每周值得分享的科技内容,周五发布。 本杂志开源(GitHub: ruanyf/weekly ),欢迎提交 issue,投稿或推荐你的项目。 周刊讨论区的帖子 《谁在招人?》 ,提供大量就业信息,欢迎访问或发布工作/实习岗位。 封面图片 美国犹他州国家公园的工作人员,进入山洞为一只冬眠的熊安装追踪项圈。不料,这头熊没有睡着,瞪大眼镜看着他们。好在它没有攻击工作人员,麻药也很快发生了作用。( 出处 ) 本周话题:信息的半衰期 半衰期是一个物理学概念,指的是放射性物质减少一半质量(辐射衰弱)所需的时间。 这个概念其实很好理解。根据爱因斯坦最著名的质能公式 E=mc²,能量即质量,任何释放能量的物体都必然会减轻质量。 最近,我看到 有人 提出了一个新的概念,他说信息也有半衰期。 信息的半衰期指的是,一半的信息量变得无关紧要或者彻底过时所需的时间。 以报纸为例,出版当天的价值最大,第二天再看,价值至少减半,所以报纸的半衰期是一天到几天。微信和微博的半衰期也类似,早上发的朋友圈,晚上就不太有人看了。至于即时消息或聊天的半衰期就更短了,只有几个小时。 当代社会的趋势是,信息的半衰期越来越短,有一个词叫做"快餐文化"。以前,一本新书可以在书店里卖几年,现在卖几个月就下架了,被新的出版物取代。大部分书籍的半衰期就是几个月到一年。 如果你写过软件文档,就会对半衰期有切身的体会。一旦软件版本升级

[译]掌握 JavaScript 面试:什么是函数式编程

坚强是说给别人听的谎言 提交于 2020-04-19 22:46:46
原文地址: Master the JavaScript Interview: What is Functional Programming? 原文作者: Eric Elliott 译文出自: 掘金翻译计划 本文永久链接: github.com/xitu/gold-m… 译者: zoomdong 校对者: Roc , Long Xiong 掌握 JavaScript 面试:什么是函数式编程 “掌握 JavaScript 面试” 是一系列的帖子,为了帮助求职者在面试中高级 JavaScript 职位时可能遇见的常见问题做准备。这些是我在真实面试场景中经常会问到的一些问题。 函数式编程已经成为 JavaScript 领域中一个非常热门的话题。就在几年前,甚至很少有 JavaScript 程序员知道什么是函数式编程,但是我在过去 3 年看到的每个大型应用程序代码库中都大量使用了函数式编程思想。 函数式编程 (通常缩写为 FP)是通过组合 纯函数 ,避免 状态共享 、 可变数据 和 副作用 来构建软件的过程。函数式编程是 声明式 的,而不是 命令式 的,应用程序状态通过纯函数流动。与面向对象编程不同,在面向对象编程中,应用程序状态通常与对象中的方法共享和协作。 函数式编程是一种 编程范式 ,这意味着它是一种基于一些基本的、定义性的原则(如上所列)来思考软件构建的方法

Searching through list of tuples

Deadly 提交于 2020-04-18 05:43:15
问题 I'm trying to write a function that accepts a string and a list of tuple pairs. I want to search through the list of tuples, and if the first value in the tuple matches the input string, I want to return the second value in the pair. I believe it functions similar to the lookup function, but I'm not sure how to implement it. Here is my thinking so far: search :: a -> [(a,b)] -> Maybe b search a (x:xs) = if a == first value in x, return second value in x -- If a is not in the list of tuples,

Decoding object with multiple constuctors with separated tags

▼魔方 西西 提交于 2020-04-16 03:28:19
问题 I have data in form of pairs of two strings, where first is the key identifying shape of the JSON delivered as the second one. fooooo {"a": 123} barrrr {"a": 123, "b": 123} fooooo {"a": 123} I would like to parse it to same data type, based on the fopooo , baasdasda1 , etc : data Test = Foo { a :: Int , b :: Int } | Bar { a :: Int } deriving (Show, Generic) In the Aeson there is a tag feature, but it seems to require presence of the tag inside the object. Is there some way to handle it like

Decoding object with multiple constuctors with separated tags

江枫思渺然 提交于 2020-04-16 03:27:07
问题 I have data in form of pairs of two strings, where first is the key identifying shape of the JSON delivered as the second one. fooooo {"a": 123} barrrr {"a": 123, "b": 123} fooooo {"a": 123} I would like to parse it to same data type, based on the fopooo , baasdasda1 , etc : data Test = Foo { a :: Int , b :: Int } | Bar { a :: Int } deriving (Show, Generic) In the Aeson there is a tag feature, but it seems to require presence of the tag inside the object. Is there some way to handle it like

haskell xml update text using HXT library

☆樱花仙子☆ 提交于 2020-04-15 17:18:10
问题 I need to have a possibility to update text in structure like this <node><data key="attr">text</data></node> . Is there any way to do this by using HXT library in haskell? 回答1: The natural answer is a lens library; I don't think hxt has such a thing associated with it. But there is xml-lens which uses xml-conduit (but not conduits in fact) The examples in the readme https://github.com/fumieval/xml-lens are fairly straightforward, but maybe a bit operator-clogged, if you aren't familiar with

haskell xml update text using HXT library

家住魔仙堡 提交于 2020-04-15 17:16:21
问题 I need to have a possibility to update text in structure like this <node><data key="attr">text</data></node> . Is there any way to do this by using HXT library in haskell? 回答1: The natural answer is a lens library; I don't think hxt has such a thing associated with it. But there is xml-lens which uses xml-conduit (but not conduits in fact) The examples in the readme https://github.com/fumieval/xml-lens are fairly straightforward, but maybe a bit operator-clogged, if you aren't familiar with

Haskell - Sum up the first n elements of a list

◇◆丶佛笑我妖孽 提交于 2020-04-15 08:22:02
问题 I´m new to Haskell. Let´s say I want to sum up the first n elements of a list with a generated function on my own. I don´t know how to do this with Haskell. I just know how to sum up a whole given list, e.g. sumList :: [Int] -> Int sumList [] = 0 sumList (x:xs) = x + sumList xs In order to sum up the first n elements of a list, for example take the first 5 numbers from [1..10] , which is 1+2+3+4+5 = 15 I thought I could do something like this: sumList :: Int -> [Int] -> Int sumList take [] =

Haskell - Sum up the first n elements of a list

ε祈祈猫儿з 提交于 2020-04-15 08:21:09
问题 I´m new to Haskell. Let´s say I want to sum up the first n elements of a list with a generated function on my own. I don´t know how to do this with Haskell. I just know how to sum up a whole given list, e.g. sumList :: [Int] -> Int sumList [] = 0 sumList (x:xs) = x + sumList xs In order to sum up the first n elements of a list, for example take the first 5 numbers from [1..10] , which is 1+2+3+4+5 = 15 I thought I could do something like this: sumList :: Int -> [Int] -> Int sumList take [] =

Create list of paths taken

匆匆过客 提交于 2020-04-14 17:15:45
问题 Given range (a,b) and lines (x,y) , I want to construct all the possible ways to cover the range with the given lines. For example with range (0,10) (if we filter list to be within range then we don't have to worry about it) and the following list (sorting it makes easier to pick next value), list = [(0,1), (1,10), (1,4), (3,5), (5,10)] I want to output list of paths taken to cover the range as follows, [ [(0,1), (1,4), (3,5), (5,10)], [(0,1), (1,10)] ] I tried setting up function that would