Monday, March 13, 2017

January 2017 1HaskellADay 1Liners

  • January 31st, 2017:
    You have d = "3.461497957769017000D+07"
    define parseDouble :: String -> Double
    (n.b.: (read d) :: Double throws an error)
    • bazzargh @bazzargh uncurry (*) (bimap read ((10^).read.(dropWhile(`elem`"0+D"))) (break (=='D') d))::Double
  • January 31st, 2017: Given
    e :: FilePath -> [String] -> [Epoch]

    readEpochs :: FilePath -> IO [Epoch]
    readEpochs f = e f . lines <$> readFile f

    point-free-itize readEpochs
    • Astynax Pirogov @alex_pir uncurry fmap . (((. lines) . e) &&& readFile
  • January 30th, 2017: Given

    parseNum :: String -> Maybe (Float, String)

    define: dropNum'' :: String -> Maybe String

    points-free in terms of parseNum
    • matt @themattchan dropNum" = fmap snd . parseNum
  • January 30th, 2017: For

    parseHeader :: String -> Maybe String
    parseHeader str = match "Start " str <|> match "Finish " str

    eliminate redundancies
    • mconcat . ([match] <*> ["Start ", "Finish "] <*>) . pure
    • Nickolay Kudasov @crazy_fizruk If you're allowed to use Monoid instead of Alternative, how about this version?
      foldMap match ["Start", "Finish"]
    • Andreas Källberg @Anka213  My solution was
      parseHeader str = foldr1 (<|>) . map (`match` str) $ ["Start", "Finish"]
      But that's longer than the original.
  • January 25th, 2017:
    given f is type: f :: Int -> a -> Bool

    for: g :: a -> Bool
    g = (||) . f 2 <*> f 27

    rewrite g using f only once in the definition
    • Denis Stoyanov @xgrommx ugly version but
      (liftA2 . liftA2) (||) ($2) ($27) f
  • January 19th, 2017:
    import Data.Tree.Merkle

    mkleaf :: Show a => a -> Leaf a
    mkleaf = uncurry Leaf . (show . hashDatum &&& id)

    redefine using (<*>)
    • Denis Stoyanov @xgrommx smth like
      mkleaf = uncurry Leaf . show . (hashDatum <$> (,) <*> id)
      mkleaf = uncurry Leaf . show . (liftA2 (,) hashDatum id)
  • January 19th, 2017:
    mkbranch1 :: Leaf a -> Branch a
    mkbranch1 = uncurry Twig . (uncurry childrenHash . (dataHash &&& dataHash) &&& id)

    redefine using (<*>)s(?)

Tuesday, February 28, 2017

February 2017 1HaskellADay Problems and Solutions

Tuesday, January 31, 2017

January 2017 1HaskellADay problems and solutions

Thursday, January 19, 2017

December 2016 1HaskellADay 1Liners

  • December 22nd, 2016:  f :: (Either a b, c) -> Either (a, c) (b, c), define f, snaps for elegance, e.g.: f (Left 4, "Hi") = Left (4, "Hi")
    • bazzargh @bazzargh uncurry (flip (join bimap . (,) ))
      • Denis Stoyanov @xgrommx need (Left 4, "Hi") = Left (4, "Hi") but your version Left ("Hi", 4)
    • Thomas D @tthomasdd Do tuple sections count? do I have access to Data.Bifunctor?
      • f (eab,c) = bimap (,c) (,c) eab
    • SocialJusticeCleric @walkstherain uncurry $ either ((Left .).(,)) ((Right .).(,))
    • Denis Stoyanov @xgrommx or f (e, a) = (join bimap (\x -> (x, a))) e
    • Nickolay Kudasov @crazy_fizruk most elegant IMO:
      f (Left a, c) = Left (a, c)
      f (Right b, c) = Right (b, c)
  • December 22nd, 2016: define a function that writes out an infinite, alternating stream of 1's and 0's as below. 
    • Philipp Maier @AkiiZedd mapM putStrLn $ join $ repeat ["0","1"]
      • Eyal Lotem @EyalL join . repeat = cycle?
    • mavant @mavant f = putStr "10" >> f
    • Eyal Lotem @EyalL mapM putStrLn $ cycle ["0","1"]
  • December 10th, 2016:
    startsWith :: [String] -> String
    points-free so that:
    startsWith ["ΜΗΛΟΝ", "ΗΔΟΝΗ"] = "ΛΟ"
    That is: (length list)+1 Char of each word
    • SocialJusticeCleric @walkstherain 
      • I prefer `uncurry (!!) . (Data.List.transpose &&& length)`
      • but `map . flip (!!) . length =<< id` only uses the Prelude
    • Nick @crazy_fizruk zipWith (!!) <*> repeat . length

Sunday, January 1, 2017

December 2016 1HaskellADay Problems and Solutions

Saturday, December 10, 2016

October 2016 1Liner 1HaskellADay problem and solutions

  • October 21st, 2016:
    You have l1 :: [(v, [(k, x)])]
    You need the transformation l2 :: [(k, [(v, x)])]
    Redistribute v and k in one line
    Props for elegance
    • Francisco T @aiceou redist xs = fromListWith (++) $ concat $ (map f xs) where f (a,ys) = map (\(x,y) -> (x,[(a,y)])) ys ... but k has to be 'Ord'

Wednesday, November 30, 2016

November 2016 1HaskellADay Problems and Solutions