Tuesday, November 9, 2021

November, 2021 1HaskellADay 1Liners

  • 2021-11-09: You have: \k _v -> f k Curry away the arguments.
  • 2021-11-09: Hello, all. It's been a minute.

    Here's a #1Liner #Haskell problem

    You have m :: Map a b

    You want to filter it by s :: Set a

    so that m has keys only in s.

    How would you do that?

    • O_O @dysinger: let map = Data.Map.fromList [(1, "one"), (2, "two"), (3, "three")]
      set = Data.Set.fromList [1,3,5,7,9]
      in Data.Map.fromList [ elem | elem <- Data.Map.toList map, Data.Set.member (fst elem) set ]
    • ephemient @ephemient: Map.filterWithKey (\k _ -> Set.member k set) map
    • ephemient @ephemient: Map.intersectionWith const map $ Map.fromDistinctAscList [(k, ()) | k <- Set.toAscList set]
    • じょお @gotoki_no_joe Map.intersection m (Map.fromSet (const ()) s)

1 comment:

Dustin said...

λ> Map.restrictKeys map set
fromList [(1,"one"),(3,"three")]