companion_cube changed the topic of #ocaml to: Discussion about the OCaml programming language | http://www.ocaml.org | OCaml 5.0 released(!!1!): https://ocaml.org/releases/5.0.0.html | Try OCaml in your browser: https://try.ocamlpro.com | Public channel logs at https://libera.irclog.whitequark.org/ocaml/
alphacentauri has joined #ocaml
dnh has joined #ocaml
dnh has quit [Ping timeout: 255 seconds]
alphacentauri has quit [Quit: WeeChat 4.0.5]
alphacentauri has joined #ocaml
alphacentauri has quit [Ping timeout: 255 seconds]
alphacentauri has joined #ocaml
azimut has quit [Ping timeout: 252 seconds]
azimut_ has joined #ocaml
xd1le has quit [Quit: xd1le]
waleee has quit [Ping timeout: 240 seconds]
waleee has joined #ocaml
ec has quit [Ping timeout: 252 seconds]
ec has joined #ocaml
waleee has quit [Ping timeout: 245 seconds]
chrisz has quit [Ping timeout: 240 seconds]
chrisz has joined #ocaml
Anarchos has joined #ocaml
Anarchos has quit [Ping timeout: 252 seconds]
TrillionEuroNote has quit [Ping timeout: 252 seconds]
TrillionEuroNote has joined #ocaml
Anarchos has joined #ocaml
<discocaml> <struktured> Can anyone think of an example of `filter_map` where separately doing the `filter` followed by the map or the `map` followed by `filter` is less efficient than a version which leverages `filter_map` directly? By more efficient, I don't mean the allocation overhead but instead some sort of computation that you only have to do once instead of twice.
<discocaml> <struktured>
<discocaml> <struktured> Every example I come up with can be typically be optimized just by changing the ordering of the operations to suit the problem.
<discocaml> <struktured> Can anyone think of an example of `filter_map` where separately doing the `filter` followed by the `map` or the `map` followed by `filter` is less efficient than a version which leverages `filter_map` directly? By more efficient, I don't mean the allocation overhead but instead some sort of computation that you only have to do once instead of twice.
<discocaml> <struktured>
<discocaml> <struktured> Every example I come up with can be typically be optimized just by changing the ordering of the operations to suit the problem.
<discocaml> <struktured> Can anyone think of an example of `filter_map` where separately doing the `filter` followed by the `map` or the `map` followed by `filter` is less efficient than a version which leverages `filter_map` directly? By more efficient, I don't mean the allocation overhead but instead some sort of computation that you only have to do once instead of twice.
<discocaml> <struktured>
<discocaml> <struktured> Every example I come up with can typically be optimized just by changing the ordering of the operations to suit the problem.
azimut_ has quit [Ping timeout: 252 seconds]
<discocaml> <darrenldl> i mean if you ignore allocation overhead, then doing `map` before `filter` seems to necessarily accommodate all of `filter_map`, including shared computation results between mapping and filtering stages
Serpent7776 has joined #ocaml
<discocaml> <struktured> That's exactly what I thought too. thanks for confirming
alphacentauri has quit [Ping timeout: 245 seconds]
alphacentauri has joined #ocaml
alphacentauri has quit [Ping timeout: 255 seconds]
alphacentauri has joined #ocaml
Serpent7776 has quit [Ping timeout: 255 seconds]
wagle has quit [Quit: No Ping reply in 180 seconds.]
wagle has joined #ocaml
alphacentauri has quit [Quit: WeeChat 4.0.5]
<discocaml> <sim642> If the filtering condition isn't a direct check on the element and gives a result to use, then you don't want to repeat it
pandeyan has joined #ocaml
anpad has quit [Ping timeout: 260 seconds]
<discocaml> <sim642> For example, instead of filtering on Map.mem and mapping to Map.find, you could filter-map with Map.find_opt. It avoids looking each element up twice in the map
Guest72425 has joined #ocaml
Tuplanolla has joined #ocaml
Guest72425 has quit [Quit: Quit]
<discocaml> <darrenldl> sure, but the idea was whatever you can ascertain during the filter phase, you could very well captured that in a `map` first, and then run it through `filter`
<discocaml> <darrenldl> could have*
<discocaml> <darrenldl> (in the above case, `... |> map Map.find_opt |> filter Option.is_some`, which is still just one lookup)
<discocaml> <darrenldl> (well okay one more map at the end, and not as clean, hm)
bartholin has joined #ocaml
alphacentauri has joined #ocaml
berberman has quit [Quit: ZNC 1.8.2 - https://znc.in]
berberman has joined #ocaml
<discocaml> <Et7f3 (@me on reply)> [ -1; 1 ] |> map (fun i -> "test".(i)) |> filter (... WTF OOB already done ...)
<discocaml> <Et7f3 (@me on reply)> then suppose we always filter before map
<discocaml> <Et7f3 (@me on reply)> [ 1; 2; 3 ] |> filter (fun x -> x mod 3 = 0) |> map (fun x -> x * 3)
<discocaml> <Et7f3 (@me on reply)> [ -1; -2 ] |> filter (fun x -> "test".(x) = 't') |> map (fun x -> ~-x) AGAIN OOB
<discocaml> <Et7f3 (@me on reply)> Oops sorry for typing in my first example. It was just to demonstrate that `map` should always be after `filter`. Then it is computational equivalent. Second example also show that doing map before filter might produce unexpected result (or we modify with mod 9)
<discocaml> <Et7f3 (@me on reply)> The last case suppose you have only negative index: the correct code would test -x throw that value and only keep boolean. Then redo map. Ok -x is cheap to compute but it is wasted computational time
Serpent7776 has joined #ocaml
haesbaert has quit [Remote host closed the connection]
dnh has joined #ocaml
bartholin has quit [Quit: Leaving]
dnh has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
aljazmc has joined #ocaml
aljazmc has quit [Remote host closed the connection]
Anarchos has quit [Quit: Vision[]: i've been blurred!]
<pgiarrusso> @Et7f3 if the map argument is pure, you can always filter after map. But it is true that you'll map more elements, so that can be much slower.
ridcully has quit [Remote host closed the connection]
<pgiarrusso> But then you could do a first map for any computation needed both by filter and map, then filter, then map again to conclude.
ridcully has joined #ocaml
<pgiarrusso> If you care for the allocations, I guess you'd need to get fusion somehow (not sure if OCaml can do that?), or fuse all by hand in a single concat_map
gareppa has joined #ocaml
gareppa has quit [Client Quit]
waleee has joined #ocaml
waleee has quit [Ping timeout: 272 seconds]
azimut has joined #ocaml
alphacentauri has quit [Quit: WeeChat 4.0.5]
alphacentauri has joined #ocaml
famubu has joined #ocaml
<famubu> Hi. I was trying to get familiar with module types in ocaml and did this: https://bpa.st/SKGQ
<famubu> But that's giving error. Does anyone know how to fix this? I'm sure it's something trivial. I've included the error as a comment in the paste.
<discocaml> <hockletock> put the type in the signature if you want it to be visible outside the module
<discocaml> <hockletock> make the second line `type t = int` rather than simply `type t`
azimut has quit [Ping timeout: 252 seconds]
<discocaml> <hockletock> if you don't want to expose the type in the signature you need to provide a way to construct values of the given type e.g. add a `val of_int : int -> t` to the sig and `let of_int = Fun.id` to the module definition
<famubu> Ah.. okay. I get it. Because of the signature, the value of `t` is opaque. Thanks!
bartholin has joined #ocaml
<discocaml> <Et7f3 (@me on reply)> pgiarrusso: does sqrt is a pure function ? Because if we map sqrt on negative integer you get garbage (complex number). So the pure criteria shouldn't come in play. So I really think we should filter before map
<pgiarrusso> for “map before filter” of course you must preserve enough info to run the filter
<discocaml> <Et7f3 (@me on reply)> The question was if we don't consider allocation overhead. So without filter_map you need first filter for precondition check then map that produce common value used by filter and map then filter and map again yo clean.
<discocaml> <Et7f3 (@me on reply)> Suppose I have function with invalid input run indefinitely I really need to filter to verify precondition
<pgiarrusso> Indeed. To me it's the same rule — I'm among those who count divergence as a side effect 😀
waleee has joined #ocaml
azimut has joined #ocaml
waleee has quit [Ping timeout: 260 seconds]
waleee has joined #ocaml
John_Ivan_ has quit [Ping timeout: 240 seconds]
John_Ivan_ has joined #ocaml
John_Ivan_ has quit [Remote host closed the connection]
John_Ivan_ has joined #ocaml
Anarchos has joined #ocaml
<discocaml> <rbjorklin> Anyone know of a good write-up for converting a library from Lwt only to also supporting Async via use of functors? If no write-up exists any recommendation of a repo that does this in a clean way would be appreciated.
<Anarchos> rbjorklin it is way over my head
Anarchos has quit [Quit: Vision[]: i've been blurred!]
<companion_cube> I don't know if it's clean but look maybe at cohttp? I think it does it (not entirely sure)
John_Ivan__ has joined #ocaml
John_Ivan_ has quit [Ping timeout: 255 seconds]
Serpent7776 has quit [Ping timeout: 240 seconds]
dnh has joined #ocaml
ec has quit [Ping timeout: 252 seconds]
ec has joined #ocaml
alphacentauri has quit [Quit: WeeChat 4.0.5]
alphacentauri has joined #ocaml
dnh has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
_whitelogger has joined #ocaml
bartholin has quit [Quit: Leaving]