unisonweb / base_v1

Unison base libraries, published using V1 codebase format
24 stars 14 forks source link

Add simple utility functions #8

Closed anovstrup closed 4 years ago

anovstrup commented 4 years ago

This Unison PR adds the following tiny utility functions, with corresponding documentation:

ignore : a -> ()
ignore _ = ()

void : (a ->{e} b) -> a ->{e} ()
void = (.) ignore

repeat : Nat -> '{e} a ->{e} ()
repeat n op =
  if n == 0 then () else
    !op
    repeat (n `drop` 1) op

forever : '{e} a ->{e} b
forever op = !op; forever op

force : '{e} a ->{e} a
force op = !op

The changes summarized below are available for you to review, using the following command:

pull-request.load https://github.com/unisonweb/base https://github.com/anovstrup/unisonweb-base

Added definitions:

 force       : '{e} a ->{e} a (+1 metadata)
 force.doc   : Doc
 forever     : '{e} a ->{e} b (+1 metadata)
 forever.doc : Doc
 ignore      : a -> () (+1 metadata)
 ignore.doc  : Doc
 repeat      : Nat -> '{e} a ->{e} () (+1 metadata)
 repeat.doc  : Doc
 void        : (a ->{e} b) -> a ->{e} () (+1 metadata)
 void.doc    : Doc
pchiusano commented 4 years ago

@anovstrup I left a comment here about how you can assign license and authorship to these: https://github.com/unisonweb/base/issues/15#issuecomment-606361945 LMK if you have any trouble.

anovstrup commented 4 years ago

@pchiusano Done. I ran into https://github.com/unisonweb/unison/issues/1364 when first adding the license/authorship links, though, because I did

.> find force forever ignore repeat void
.> link author 1-10
.> link license 1-10

forgetting that the first link command changed the meaning of the numbers. Then, because I glossed over the link output, I didn't even realize my mistake until I did a pull-request.load to check everything before pinging you. 😬

pchiusano commented 4 years ago

@anovstrup cool! will take a look at this tomorrow I think. Was tracking down https://github.com/unisonweb/unison/pull/1389 which I was concerned might affect these PRs. (Conclusion: it shouldn't in any harmful way, and we'll deploy the fix in next release)

pchiusano commented 4 years ago

@anovstrup awesome, thanks! added in https://github.com/unisonweb/base/commit/933481708caa5d83cab07e6cdf01c1a467ddc015 congrats on being the first outside new contributor to base!! (I missed cherry-picking repeat, added afterwards here)

A couple notes:

We are still working out conventions for library namespaces and organizing contributions to libraries. I do have a suggestion though. When adding new definitions, do it in a separate parallel namespace, so create .prs.base.myCoolPR (initially empty, but will have definitions with parallel naming to .base, so if you write a new function, List.frobnicate, it goes in prs.base.myCoolPR.List.frobnicate).

When you're done and ready to issue the PR, you can assign license info in bulk (just find in prs.base.myCoolPR, then link mylicense 1-8 or whatever). You can also squash the history and discard patches using copy to a fresh namespace.

You can then merge .prs.base.myCoolPR .base if you like. For new definitions, you don't necessarily need to even do the merge locally, this would work as well:

.> pull-request.create https://github.com/unisonweb/base https://github.com/anovstrup/unisonweb-base:.prs.base.myCoolPR
anovstrup commented 4 years ago

@pchiusano Thanks, Paul! FWIW, here's the context where void proved useful for me:

Stream.take : Nat -> '{Stream a} r ->{Stream a} ()
Stream.take n s = Stream.pipe (void s) '(repeat n '(Stream.emit Ask.ask))
-- where Stream.pipe: '{Stream a} r -> '{Ask a, Stream b} r ->{Stream b} r ; note 
-- that this pipe differs from the one used in the abilities tutorial, supporting   
-- use cases like logging where the result of the computation is interesting

Of course ignore . s is only slightly more to type, but I feel like void s expresses the intent more clearly here (especially to readers familiar with Haskell's void).

I'm totally cool with holding off on including void in base but wanted to give you the data point.