0

I want to do what I think is the simplest thing, map through a plain old Javascript object, using fp-ts constructs.

There are a lot of operators I would like to use — map, filterMap, and so on — but they seem to require the Map type (which I assume is the Javascript Map object) — and there does not seem to be any easy way to convert back and forth between Map and Javascript object, which in Typescript is the standard representation of algebraic data types and of its Record<K,V> type.

This seems like a pretty big hole. Please don’t tell me I have to switch to lodash...

8
  • I don't know fp-ts, however "mapping an object" should mean "I execute a function with the object as parameter". So, I'd expect map(f, obj) to be basically the same as f(obj). With this in mind, you could use a functor for the object which would allow you to call map on it. Again, I don't know fp-ts but it should have an identity functor under some name.
    – VLAZ
    Feb 13, 2022 at 8:34
  • @VLAZ — the issue isn’t the name; fp-ts has several functors that do the things I want done; it just does them to types other than object. There is a map for List and for Map, just none for objects. Feb 13, 2022 at 15:46
  • Wait, when you say you want to map objects, what do you mean? You want to transform one whole object or you want to transform each value of an object?
    – VLAZ
    Feb 13, 2022 at 15:50
  • @VLAZ — different things, but for example, I would like map(v => v+1)({a: 6}) to compile (which it does not do now) and yield {a: 7}. map(v => v+1)(new Map([['a', 6]])) does work and does yield Map([['a', 7]]) but is not what I want. Ps. Why does Javascript have a concept called map and another called Map and they are almost totally different? How about “AssociativeArray” or “Dictionary”? Feb 13, 2022 at 16:28
  • Both terms come from other places and relate to the same concept. But in different ways. A Map defines a data structure that associates keys with values. Java also calls this a Map. But some languages do call it other things like C# has a Dictionary but it's exactly the same concept. JS is neither the first, nor would be the last language that uses this term. While .map() defines an association of an item with another item via a function. You can think of capital M Map is a noun, while lowercase m map is a verb. One is a
    – VLAZ
    Feb 13, 2022 at 16:50

1 Answer 1

3

fp-ts/Record lets you work with Record<K, V> as if they were a Functor in V, so you can have

import { map } from 'fp-ts/Record'

const mapped = map((v: number) => v + 1)({a: 1}) // { a: 2 }
2
  • Record. I should have guessed. Feb 14, 2022 at 18:17
  • there is also Struct for objects with no index, but that does not have map. I think it only has evolve and is meant for keeping the same type rather than mapping the type into another. I think Mar 17, 2022 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.