Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'containers' from dependencies #30

Open
chshersh opened this issue Jul 9, 2018 · 2 comments

Comments

@chshersh
Copy link
Member

commented Jul 9, 2018

To remove containers from dependencies we need to rewrite two functions:

  • unionWith
  • fromSortedList

unionWith

Rewrite this to something more efficient:

  1. Allocate new mutable vectors of length n+m.
  2. Traverse input vectors simultaneously, copying elements in the right order. We should be able to take advantage of the fact that the elements are in a particular order. For regular sorted vectors it's very simple, for our cache-friendly order it can be tricky, but should be possible (I hope)
  3. While doing this, count the actual amount of elements copied (it can be less than n+m due to conflicts). Shrink the mutable vector.
  4. Freeze and return.

fromSortedList

Rewrite into the following way:

  1. Allocate two mutable arrays of length n: one for original list and second one for result.
  2. Rewrite current algorithm inside monad and change array in-place.
  3. Freeze and return.
chshersh added a commit that referenced this issue Aug 20, 2018
* [30] Rewrite fromSortedList to use arrays

* Improve more

* Use unsafeFreezeArray in adjust
@vrom911 vrom911 added this to To do in #2: Hacktoberfest (October, 2018) via automation Sep 30, 2018
@chshersh chshersh removed the Hacktoberfest label Nov 7, 2018
@C37H40O9

This comment has been minimized.

Copy link

commented Dec 14, 2018

What other functions, besides const, can be used with unionWith?

@int-index

This comment has been minimized.

Copy link
Collaborator

commented Dec 14, 2018

What other functions, besides const, can be used with unionWith?

For the Identity-parametrized version of unionWith,

unionWith :: (forall x. x -> x -> x) -> TMap -> TMap -> TMap

we may use

  1. \x _ -> x to prefer elements from the first map (left-biased union)
  2. \_ x -> x to prefer elements from the second map (right-biased union)

For the f-parametrized version of unionWith,

unionWith :: (forall x. f x -> f x -> f x) -> TypeRepMap f -> TypeRepMap f -> TypeRepMap f

When f is a GADT, we can match. For instance,

data F a where
  FInt :: Int -> F Int
  FBool :: Bool -> F Bool

With this, we can write more interesting combining functions:

combine (FInt a) (FInt b) = FInt (a + b)
combine (FBool a) (FBool b) = FBool (a || b)

We can make both versions more useful by providing the Typeable key to the caller:

unionWith :: (forall x. Typeable x => x -> x -> x) -> TMap -> TMap -> TMap
unionWith :: (forall x. Typeable x => f x -> f x -> f x) -> TypeRepMap f -> TypeRepMap f -> TypeRepMap f

This should be possible after #49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.