方便的F#代码段 [英] Handy F# snippets

查看:62
本文介绍了方便的F#代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经有两个 有关F#/功能代码段的问题

但是我我在这里寻找的是有用片段,可重用的小辅助功能。或是您永远不会记得的晦涩但漂亮的模式。

However what I'm looking for here are useful snippets, little 'helper' functions that are reusable. Or obscure but nifty patterns that you can never quite remember.

类似的东西:

open System.IO

let rec visitor dir filter= 
    seq { yield! Directory.GetFiles(dir, filter)
          for subdir in Directory.GetDirectories(dir) do 
              yield! visitor subdir filter} 

我想将其作为一种方便的参考页。因此,没有正确的答案,但希望有很多好的答案。

I'd like to make this a kind of handy reference page. As such there will be no right answer, but hopefully lots of good ones.

编辑 Tomas Petricek创建了一个专门针对F#代码段的网站< a href = http://fssnip.net/ rel = nofollow noreferrer> http://fssnip.net/ 。

EDIT Tomas Petricek has created a site specifically for F# snippets http://fssnip.net/.

推荐答案

中缀运算符

我从 http://sandersn.com/blog//index.php/2009/10/22/infix -function-trick-for-f 转到该页面以获取更多详细信息。

I got this from http://sandersn.com/blog//index.php/2009/10/22/infix-function-trick-for-f go to that page for more details.

如果您知道Haskell,您可能会发现自己在F#中缺少中缀糖:

If you know Haskell, you might find yourself missing infix sugar in F#:

// standard Haskell call has function first, then args just like F#. So obviously
// here there is a function that takes two strings: string -> string -> string 
startsWith "kevin" "k"

//Haskell infix operator via backQuotes. Sometimes makes a function read better.
"kevin" `startsWith` "K" 

而F#没有'infix'运算符,几乎可以通过管道和'backpipeline'(谁知道这样的事情?)来完成相同的事情

While F# doesn't have a true 'infix' operator, the same thing can be accomplished almost as elegantly via a pipeline and a 'backpipeline' (who knew of such a thing??)

// F# 'infix' trick via pipelines
"kevin" |> startsWith <| "K"

这篇关于方便的F#代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆