Skip to content

mconcat #1

Open
Open
@jamesdbrock

Description

@jamesdbrock

James Brock 8:47 AM
In Haskell we have the mconcat method of Monoid just so we can append lists of strings together in O(n) instead of O(n²). https://hackage.haskell.org/package/base-4.14.1.0/docs/Data-Monoid.html#v:mconcat
What is the equivalent in PureScript?

hdgarrood 8:52 AM
fold is the PureScript equivalent of mconcat
8:53
It doesn't have any tricks for appending strings, but I don't think you need them here, because strings aren't linked lists in JS and because JS VMs are clever

James Brock 8:54 AM
really? fold (xs :: NonEmptyList String) will be O(n) you think?

hdgarrood 8:55 AM
I'm not certain, but you should try it if you're unsure

James Brock 8:56 AM
ok thx

hdgarrood 8:56 AM
There's also joinWith from Data.String, which is the same as JS's array.join(sep) (edited)

James Brock 9:03 AM
Suppose I were to benchmark fold (xs :: NonEmptyList String) and found it were O(n²). Would it be possible to use instance chains to write a specialization for foldableNonEmpytListString :: Foldable1 (NonEmptyList String) ?
https://github.com/purescript/purescript-lists/blob/62900a56f6864638c952575dfd211a1cc55be7da/src/Data/List/Types.purs#L238

hdgarrood 9:06 AM
I'm fairly sure it is possible, but whether or not that would be merged is a separate matter

9:07
Please do let us know if it is quadratic, it's not something I'd considered before

9:10
I'm fairly sure repeated uses of += to build up a string in JS isn't quadratic, but of course when it's being implemented generically via Foldable it might not work out as well

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions