方便的 F# 片段 [英] Handy F# snippets

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

问题描述

已经有两个 问题关于 F#/功能片段.

There are already two questions about F#/functional snippets.

然而,我在这里寻找的是有用的片段,可重用的小辅助"功能.或者你永远无法完全记住的晦涩但漂亮的模式.

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# 片段创建了一个站点 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# 没有真正的中缀"运算符,但同样的事情可以通过管道和反向管道"几乎同样优雅地完成(谁知道这样的事情??)

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天全站免登陆