ã³ã³ãã¥ãã¼ã·ã§ã³å¼ã§ãã¼ã¯ã¼ãå¼æ°
ããã¯F# Advent Calendar 2021ã®23æ¥åã®è¨äºã§ãã
ãã®éã®ç¬¬3å FUN FAN F#ã«åå ããã¨ãããã«ã¹ã¿ã ãªãã¬ã¼ã¿ã¼ããã¼ã¯ã¼ãå¼æ°ã®ããã«ä½¿ã£ã¦ããã®ãã¿ã¦æãã¤ãããã¿ãç´¹ä»ãã¾ãã ããã¾ã§ãã¿ã§ãã®ã§ãå®éã«ãã®ãããªAPIãæä¾ããã®ãã©ããã®å¤æã¯åèªã«ä»»ãã¾ãã
ã«ã¹ã¿ã ãªãã¬ã¼ã¿ã¼ã®èª¬æã¯ãã«ã¹ã¿ã ãªãã¬ã¼ã·ã§ã³ãã³ãºãªã³ãªã©ãåèã«ãã¦ããã ãã¨ãã¦ãä»åç®æãã®ã¯ä»¥ä¸ã®ãããªAPIã§ãã
let str = "F# Advent Calendar 2021" let res1 = str |> substring { start 3; end_ 9 } let res2 = str |> substring { start 19; length 4 } printfn "%s, %s" res1 res2 // => Advent, 2021
ããã ãã ã¨ã¤ã¾ããªãã®ã§ã次ã®ãããªãã®ã¯ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãããã§ãã
// end_ã¨lengthã¯ä¸¡æ¹æå®ã§ããªãããã«ããã substring { start 3; end_ 9; length: 10 } // startããªãã¨ã¨ã©ã¼ã«ããã substring { end_ 9 } // åããã¼ã¯ã¼ããè¤æ°æå®ãã¦ã»ãããªã substring { start 3; end_ 9; start: 4 }
ã¾ããé ä¸åã«ãããã§ãããã
// 次ã®2ã¤ã¯åã substring { start 3; end_ 9 } substring { end_ 9; start 3 }
èãæ¹
ã³ã³ãã¥ãã¼ã·ã§ã³å¼ã§ã¯ãRun
ãæä¾ãããã¨ã§ã³ã³ãã¥ãã¼ã·ã§ã³å¼ã§çµã¿ç«ã¦ãçµæã Run
ã®å¼æ°ã«æ¸¡ããããã«ãªãã¾ãã
ã¨ãããã¨ã§ãRun
ããªã¼ãã¼ãã¼ããã¦åã«ãã£ã¦å¦çãåãåããã¨ãããã¨ãã§ãã¾ãã
ããã¦ãã«ã¹ã¿ã ãªãã¬ã¼ã·ã§ã³ã§è¨±ãããçµã¿åãããåã§æå®ã§ããã°ããã以å¤ã®çµã¿åããã¯ã¨ã©ã¼ã«ã§ãã¾ãã ãã®æ¹éã§ä½ã£ã¦ããã¾ãã
ä½ãæ¹
ã¾ãã¯ãè¨å®ããã¦ããªããã¼ã¯ã¼ãã表ãåãç¨æãã¾ãã
type NotSet = NotSet
次ã«ãã«ã¹ã¿ã ãªãã¬ã¼ã·ã§ã³ã使ãã®ã§ããã«ãã¼ã¯ã©ã¹ã« Yield
ãå¿
è¦ã§ãã
member _.Yield (()) = (NotSet, NotSet, NotSet)
Yield
ã¯ã¿ãã«ãè¿ãããã«ãªã£ã¦ãã¦ãåããé ã« start
, end_
, length
ã«å¯¾å¿ãã¾ã*1ã
å½ç¶ãåæ段éã§ã¯ã©ã®ãã¼ã¯ã¼ããæå®ããã¦ããªãããããã¹ã¦ NotSet
ã§ãã
次ã«ãã«ã¹ã¿ã ãªãã¬ã¼ã·ã§ã³ãã¨ã«å¯¾å¿ããã¿ãã«ã®è¦ç´ ãæ´æ°ãã¾ãã ä¾ãã°ãããªæãã§ãã
[<CustomOperation("end_")>] member _.End((startParam: 'a, _: NotSet, lengthParam: NotSet), end_: int) = (startParam, end_, lengthParam)
ã¿ãã«ã®æåã®è¦ç´ (startParam
) 㯠end_
ãå¼ã³åºãåã§ãå¾ã§ãè¨å®ããã¦ãããã°ããã®ã§ããªãã§ãåãåããããã«ãã¦ãã¾ãã
ã¿ãã«ã®2çªç®ã®è¦ç´ ã¯ãend_
èªä½ã§ãã®ã§ãã㯠End
ã®ç¬¬2å¼æ°ã使ãããã使ãã¾ããã
ãªã®ã§ã_
ã§åãã¦ãã¾ãã
ã¾ããåã NotSet
ã«éå®ãã¦ãã¾ããããããéå®ããã« 'b
ã¨ãããã¨ã§ãä½åº¦ãè¨å®ã§ãã¦ãã¤å¾åã¡ã«ãªããããªAPIãæä¾ã§ãã¾ãã
ã¿ãã«ã®æå¾ã®è¦ç´ (lengthParam
)㯠NotSet
ã«éå®ãã¦ãã¾ãã
ããã¯ãend_
ãªãã¬ã¼ã¿ã¼ãå¼ã³åºãåã« length
ãªãã¬ã¼ã¿ã¼ãå¼ã³åºãã¦ããå ´åãã³ã³ãã¤ã«ã¨ã©ã¼ã«ãããããã§ãã
ãã®ããã«ã
- ãã®ãªãã¬ã¼ã¿ã¼ãå¼ã³åºãããå ´åã«ã¨ã©ã¼ã«ããããã¼ã¯ã¼ãã¯
NotSet
ãæå® - ãã®ãªãã¬ã¼ã¿ã¼ãå¼ã³åºãããå ´åã«ã¨ã©ã¼ã«ããããªããã¼ã¯ã¼ãã¯åãã©ã¡ã¼ã¿ã¼ãæå®
ãããã¨ã§ãå¤ãªçµã¿åãããå¼¾ããããã«ãã¦ãã¾ãã
æå¾ã«ãé©åãªçµã¿åããã®å ´åã«é©åãªå¦çããã Run
ãå®ç¾©ãã¦å®æã§ã*2ã
member _.Run((start: int, end_: int, _: NotSet)) = fun (str: string) -> str.Substring(start, end_ - start) member _.Run((start: int, _: NotSet, length: int)) = fun (str: string) -> str.Substring(start, length)
å ¨ä½
å ¨ä½ã¯ãããªæãã§ãã
type NotSet = NotSet type SubstringBuilder () = member _.Yield (()) = (NotSet, NotSet, NotSet) [<CustomOperation("start")>] member _.Start ((_: NotSet, endParam: 'b, lengthParam: 'c), start: int) = (start, endParam, lengthParam) [<CustomOperation("end_")>] member _.End ((startParam: 'a, _: NotSet, lengthParam: NotSet), end_: int) = (startParam, end_, lengthParam) [<CustomOperation("length")>] member _.Length ((startParam: 'a, endParam: NotSet, _: NotSet), length: int) = (startParam, endParam, length) member _.Run((start: int, end_: int, _: NotSet)) = fun (str: string) -> str.Substring(start, end_ - start) member _.Run((start: int, _: NotSet, length: int)) = fun (str: string) -> str.Substring(start, length) let substring = SubstringBuilder ()
ãã¿ã°ããï¼
ãæ°ã¥ãã®æ¹ãããããããã¾ããããããã¯åå®å ¨ãªBuilderãã¿ã¼ã³ãã³ã³ãã¥ãã¼ã·ã§ã³å¼ã§å解éãããã®ã§ããã åå¶ç´ã¨ã使ããªãã¦ããã®ã§ããªãªã¸ãã«ããã大åã·ã³ãã«ã«å®è£ ã§ãã¦ãããããªæ°ã¯ãã¾ãã
ä½è«
ä»æ¥ã§35æ³ã«ãªã£ã¦ãã¾ãã¾ããã ããã°ã©ãã®å®å¹´ãªã®ã§ãå¹´éãã ããã
追è¨
å¹çåã¨ã¨ã©ã¼ã¡ãã»ã¼ã¸ãæ¹è¯ãããã¼ã¸ã§ã³ã«ã¤ãã¦ã®è¨äºãæ¸ãã¾ããã