Tuesday, February 6, 2018

January 2018 1Liner 1HaskellADay problems and solutions

  • January 8th, 2018: from Nicoλas‏ @BeRewt
    A small @1HaskellADay, old-school. Define foo:

    > foo 3 [1..5]
    [([1,2,3], 4), ([2,3,4], 5)]

    > foo 2 [1..4]
    [([1,2], 3), ([2,3], 4)]

    > foo 2 [1..20]
    [([1,2],3), ([2,3],4), ..., ([18,19],20)]

    > foo 20 [1..2]
    []
    • Demiurge With a Teletype @mrkgrnao
      foo n
        = tails
        # filter (length # (> n))
        # map (splitAt n # second head)

      (#) = flip (.)
    • Andreas Källberg @Anka213
      I haven't tested it, but this should work:
      foo n xs = [ (hd,x) | (hd , x:_) <- n="" splitat=""> tails xs ]
    • <- n="" splitat="">Nicoλas @BeRewt foo n = zip <$> fmap (take n) . tails <*> drop n
  • January 5th, 2018: You have the following DAG-paths:

    a -> b -> c -> e
    a -> b -> d -> e
    q -> r -> s
    w -> x
    y -> z

    and many more.

    From a path, provide a bi-directional encoding* given maximum graph depth is, say, 7, max number of roots is, say, 10, and max number of nodes is, say, 1000.
    • *bi-directional encoding of a graph path:

      DAG path -> enc is unique for an unique DAG path
      enc -> DAG path yields the same DAG path that created the unique enc.

      *DAG: "Directed, acyclic graph."
  • January 5th, 2018: given s :: Ord k => a -> (k,[v])

    define f using s

    f :: Ord k => [a] -> Map k [v]

    with no duplicate k in [a]
    • Christian Bay @the_greenbourne f = foldr (\e acc -> uncurry M.insert (s e) acc) M.empty
      • me: you can curry away the acc variable easily
      • Christian Bay @the_greenbourne You're right :)
        f = foldr (uncurry M.insert . s) M.empty
    • Bazzargh @bazzargh fromList.(map s) ?
      • me: Yuppers

Wednesday, January 31, 2018

January 2018 1HaskellADay Problems and Solutions

Friday, January 5, 2018

December 2017 1HaskellADay 1Liners problems and solutions

  • December 29th, 2017:
    given f :: Monad m => n -> a -> m (Maybe b)
    define g :: Monad m => n -> a -> m (a, Maybe b)
    using f and ... arrows? Kleisli category?
    • Bazzargh @bazzargh (\n a->liftM ((,) a) (f n a)) ... according to pointfree.io, that's `liftM2 fmap (,) . f` but I can't pretend to get the transformation
  • December 29th, 2017:
    given f :: a -> b
    define g :: [a] -> [Maybe c] -> [(b, c)]

    >>> g [1,2,3] [Just 7, Nothing, Just 10]
    [("1",7),("3",10)]

    when f = show
    • matt @themattchan
      g = catMaybes ... zipWith (fmap . (,) . f)
      where (...) = (.).(.)
    • garrison @GarrisonLJ g a b = map (f.id***fromJust) . filter (isJust . snd) $ zip a b
    • TJ Takei @karoyakani g = (catMaybes .) . zipWith ((<$>) . (,) . f)
  • December 29th, 2017: define f :: [(a,b)] -> ([a], [b])
    • Андреев Кирилл @nonaem00 and matt @themattchan unzip
    • Victoria C @ToriconPrime f = fmap fst &&& fmap snd
      • (in a vacuum, a more general type signature would be inferred, but the compiler limits itself as instruct)

Tuesday, January 2, 2018

December 2017 1HaskellADay problems and solutions

Friday, December 29, 2017

November 2017 1HaskellADay 1Liner problem and solutions

  • November 5th, 2017: f :: Map Int [a] -> [b] - > [(Int, b)]
    for, e.g.: f mapping bs
    length bs == length (concat (Map.elems mapping))
    define f
    • Andreas Källberg @Anka213 Using parallel list comprehensions:
      f mp bs = [ (k,b) | (k,as) <-assocs mp, a <- as | b <- bs]
    • Steve Trout @strout f = zip . foldMapWithKey (fmap . const)

Thursday, November 30, 2017

November 2017 1HaskellADay problems and solutions

Saturday, November 4, 2017

October 2017 1Liner 1HaskellADay problems and solutions

  • October 20th, 2017:
    You have a list of numbers: [1,2,3,4]
    You have a list of the same length of number fns: [succ, id, id, succ]
    You want: [2,2,3,5]
    •  🇪🇺 Cλément D  🌈  🐇 @clementd zipWith (flip ($)) ?
      •  he adds: `zipWith (flip id)` is a bit shorter tho
    • Simon Courtenage @SCourtenage zipWith ($) [succ,id,id,succ] [1,2,3,4]
    • lukasz @lukaszklekot getZipList $ ZipList [succ, id, id, succ] <*> ZipList [1, 2, 3, 4]
    • Alexey Radkov @sheshanaag (map (uncurry ($)) .) . zip
  • October 5th, 2017: "reverse the sequencing"
    You have [[(1,2),(1,3),(1,7)],[(9,2)],[(11,3)]]
    You want [(1,[2,3,7]),(9,[2]),(11,[3])]
    • bazzargh @bazzargh map ((,) <$> head.(map fst) <*> (map snd))
    • bazzargh @bazzargh map ((first head).unzip)
    • Chris Martin @chris__martin \x -> [(a, b : fmap snd xs) | Just ((a, b) :| xs) <- fmap="" li="" nonempty="" x="">
    • Simon Courtenage @SCourtenage fmap (\x -> (fst . head $ x, fmap snd x))
      • Denis Stoyanov  🐜 @xgrommx Your solution nice) but u can do it with point free style like
        • fmap(fst.head &&& fmap snd)
    • Denis Stoyanov  🐜 @xgrommx My solution is ugly, but I wanna to solve it with traverse)
      • fmap(first head . traverse (first (:[])))
    • Andreas Källberg @Anka213 map$fst.head&&&map snd
    • Scott Fleischma‏ @scottfleischman
      traverse
        $ _1
          (\case
              [y] -> Just y
              _ -> Nothing
          . nub
          )
        . unzip
        :: [[(Int, Int)]] -> Maybe [(Int, [Int])]
    • Scott Fleischman @scottfleischman
      let
    •  sing [] = Left "Too few"
       sing [x] = Right x
       sing (_ : _) = Left "Too many"
       valid = sing . nub
       go = _1 valid . unzip
      in traverse go
    • matt @themattchan map ((head *** id ) . unzip)
  • October 3rd, 2017:
    you have [(1,[2,3,4]),(10,[5,6,7])]
    you want [(1,2),(1,3),(1,4),(10,5),(10,6),(10,7)]

    or, generally: [(a,[b])] -> [(a,b)]

    Go!

    • bazzargh @bazzargh (uncurry (zip . repeat) =<<)
    • Bruno @Brun0Cad (=<<) sequence
    • Denis Stoyanov  🐜 @xgrommx fmap (uncurry (liftA2(,) . (:[])))
      • Darren G @Kludgy I like that this doesn't unnecessarily implicate the sequentiality of bind.
    • Darren G @Kludgy Funny this same product came up at work last week.
      concatMap $ \(a,bs) -> fmap (\b -> (a,b)) bs