Last active
May 26, 2016 12:38
-
-
Save tokiwoousaka/87fdfa3ae1f1de8024459f4a6199f4ff to your computer and use it in GitHub Desktop.
ちゅーんさんにとって本当に必要だったもの
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
{-# LANGUAGE RankNTypes #-} | |
module Main where | |
import Control.Lens | |
import Data.List | |
finding :: (a -> Bool) -> Lens' [a] (Maybe a) | |
finding f = lens (find f) $ setToList f | |
setToList :: (a -> Bool) -> [a] -> (Maybe a) -> [a] | |
setToList _ xs Nothing = xs | |
setToList _ [] (Just y) = [] | |
setToList f (x:xs) m@(Just y) = | |
if f x then y : xs else x : setToList f xs m | |
main :: IO () | |
main = do | |
print $ [1..10]^.finding (==5) -- Just 5 | |
print $ [1..10]&finding (==5)._Just.~999 -- [1,2,3,4,999,6,7,8,9] | |
print $ [1..10]^.finding (==0) -- Nothing | |
print $ [1..10]&finding (==0)._Just.~999 -- [1,2,3,4,5,6,7,8,9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment