Git Product home page Git Product logo

prelude-ls's Introduction

prelude.ls Build Status

is a functionally oriented utility library. It is powerful and flexible. Almost all of its functions are curried. It is written in, and is the recommended base library for, LiveScript.

See the prelude.ls site for examples, a reference, and more.

You can install via npm npm install prelude-ls

Development

make test to test

make build to build lib from src

make build-browser to build browser versions

prelude-ls's People

Contributors

apaleslimghost avatar audreyt avatar chrisvfritz avatar dependabot[bot] avatar gkz avatar jampekka avatar johngeorgewright avatar michaelficarra avatar paulmillr avatar pdehaan avatar texastoland avatar vendethiel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

prelude-ls's Issues

Change sortBy

Hey,

I thought about changing sortBy. Imho, its current name isn't describind what it does. I think it should be called sortWith
And maybe a sortBy function :

sort-by = (prop, list) --> list.sort (a, b) -> 
  | a[prop] < b[prop] => -1
  | a[prop] > b[prop] => 1
  | otherwise         => 0

Add unfold

Pretty useful dual to fold. Also, in prelude.hs, as you know.

bower.json and component.json

Need for bower and component integration.
Now, when I include prelude-ls to my bower.json (brunch.io based project), result app.js contains all unwrapped scripts from prelude-ls folder, include tests.

clone / deep copy

would it be useful to have that fn since js doesn't have great immutability support out-of-box?

some-complex-dict |> clone |> transform (x y z)

lodash impl.

installPrelude not exported

When compiling LiveScript with -d flag, it inserts a code snippet on the top to install prelude to global namespace. But the install method call fails.

LiveScript version is 1.1.1
prelude version 1.0.3

/sandbox/lib/index.js:2
  require('prelude-ls').installPrelude(global);
                        ^
TypeError: Object #<Object> has no method 'installPrelude'

Support harmony generators

Now that Harmony generators have landed in V8 (behind a flag, but still), we should add syntax for them. I suggest -*> as a parallel to function*.

Bind context of curried function

How to bind this to a function, like concat-map for example? I tried the following with no luck:

(concat-map.bind @) f, @x
concat-map.call @, f, @x
(concat-map.call @, f) @x

Any ideas? I see that curry$ helper keeps track of the context, but how to change it?

Use List and Obj functions on same symbol?

It seems when I try to use overloaded functions like map I can only have the List or the Obj version on the map symbol. Is there any way to make this work like underscore, where both use the same symbol?

Str.repeat not referenced from the exported prelude object

Hello,

It's not a big issue since it doesn't prevent from doing anything, it's just that I've been surprised to notice that the following reference:

prelude.repeat

was undefined, forcing me to use:

prelude.Str.repeat

while, there isn't any name collision with other sub-module (cf doc, section Function reference).

Maybe for a future repeat in List?

PS: of course the previous snippets consider the following code has been executed before:

require! prelude: 'prelude-ls'

concatMap

The doc example is wrong.

concatMap (+ 2), [[1 2] [3]]

returns

21,223

I think it's much better if we could find a way to generate documentation from tests.

That is the only way to keep everything in sync with no manual checks.

Adding code means adding tests/docs automatically.

Mocha is doing this pretty ok.

But you see the assertions.

Nice to have functions

# checking for null and undefined
const item =
  test |> maybe (.maybe-not-exists-prop)  |> maybe (.deeply-prop)

# return new object with additional properties
const item = 
   test |> extend (new-prop: 1, new-prop2: 2)


cons function

Would you be prepared to add a cons to the prelude? maybe

cons = (x,xs) -> [x] ++ xs

zip is not a curried function?

It doesn't seem that http://gkz.github.com/prelude-ls/#f-zip is a curried function.

Does this mean I cannot do:

array2 |> map zip array1 |> ...

Tried it without success.

Think it has to do that it is not a curried function.

If so, could it be done that zip takes at least 2 arrays (seems pointless to take one).

If only one is supplied, then it returns a partial function.

What do you think?

prelude-browser is unusable

the minified version does not attach prelude to anything, and the unminified version just errors out. Also the website says to use npm install prelude-ls and then point to the browser version, but the browser version is not include in the npm package.

fix fix

I noticed a minor issue in the definition of fix - it takes an additional, unnecessary argument. The definition should read:

fix = (f) ->
  ( (g) -> -> f(g g) ...&) do
    (g) -> -> f(g g) ...&

Unique By Curry Bug

// javascript
var q = [ 'a', 'abc', 'z', 'chaoeu' ];
uniqueBy(function(it){return it.length;}, q); // works
uniqueBy(function(it){return it.length;})(q); // TypeError: Cannot read property 'length' of undefine

var x = curry(uniqueBy);
x(function(it){return it.length;})(q); // works again

livescript

here's a simple workaround until it's fixed:

prelude-has-a-bug-in-the-uniqueby-function = curry unique-by

prelude doesn't install

After running a file with livescript -d program.ls :

Failed at: program.ls
TypeError: Object #<Object> has no method 'installPrelude'
    at Object.<anonymous> (/home/cdc/code/program.ls:2:25)
1| if (typeof window == 'undefined' || window === null) {
2+   require('prelude-ls').installPrelude(global);
3| } else {
4|   prelude.installPrelude(window);
5| }
6| 'use strict';
~/node_modules/prelude-ls $ grep -riP "installPrelude" .
~/node_modules/prelude-ls $

So basically, the installPrelude function was removed in the prelude refactor, but the corresponding code hasn't been updated in livescript -d yet.

take-while (drop-while) doesn't take (drop) the last element

I just hit this and thought I was probably just too tired to code or something, but after narrowing down the problem I figured that there is a serious bug with take-while and drop-while:

livescript> take-while (== 42), [42]
[]
livescript> take-while (== 42), [42 42 42]
[ 42, 42 ]
livescript> drop-while (== 42), [42]
[ 42 ]
livescript> drop-while (== 42), [42 42 42]
[ 42 ]

Looking at the generated javascript code for take-while:

for (i$ = 0; i$ < len; ++i$) {
  i = i$;
  if (!p(xs[i])) {
    break;
  }
}
return xs.slice(0, i);

len is actually never assigned to i because the condition i$ < len would fail first, and xs.slice 0 takes i which is len - 1 if all tests are successful and would take one less element. The same problem exists for drop-while as well.

My proposal is something like this:

find-index = (p, xs) -->
    for i til xs.length
        return i if p xs[i]
    return xs.length

take-while = (p, xs) --> xs.slice 0, (find-index ((not) << p), xs)

drop-while = (p, xs) --> xs.slice (find-index ((not) << p), xs)

Add `on` combinator?

In Haskell, there is a very useful combinator in Data.Function called on which is defined as:

on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
f `on` g = \x y -> g x `f` g y

translated to Livescript:

on = (f, g, x, y) --> (g x) `f` (g y)
same-length = (==) `on` (.length)
same-length [1 to 5] [6 to 10] # true

Unfortunately, this won't work because on is already a keyword in Livescript (an alias of true), so we have to find a new name for that.

Create JS/CS extension

After creating an extension system ala underscore.js #45

Add various functions which cover functionality that is present in LiveScript, but not in JS/CS

Non-minified version for browser

Could you please also be making a non-minified version for browser? And including it to npm package so it can be read by node.js?

With brunch, minifying is only done in production envs (automatically), and in dev env it's useful to use non-minified files because of cool traces etc.

K Combinator

Would like to have the K combinator, or the function known as const in Haskell, in prelude.ls. Since const is already a keyword in LiveScript, I would suggest simply using k or something else for its name.

The implementation should be straight forward in LiveScript: k = (x, y) --> x or k = (x) -> (y) -> x for better performance.

add categorize function

Add categorize function like:

categorize =  (f, xs) --> 
  f = objToFunc f if typeof! f isnt \Function
  type = typeof! xs
  result = {}
  if type is \Object
    for key, x of xs
      category = f x
      result[category] ?= {}
      result[category][key] = x
  else
    for v in xs
      k = f v 
      result.(k) ?= []
      result.(k).push v
  result

I think that should be self explanatory and the the usefulness also pretty obvious. You like?

Create extension system

So that extensions to prelude can be written and easily integrated. Something like in underscore.js

Nice to have function - parse arguments

const get-args = (fn)->
   const s = fn.to-string!.replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/g, "")
   s.slice(s.index-of(\() + 1, s.index-of(\))).match(/([^\s,]+)/g) ? []


#examples

get-args ($scope, $route-params) -> #["$scope","$routeParams"]

get-args -> #[]

Who use angular directives should like it

Because then

const body = (fn)->
   get-args(fn) ++ [fn]

Intersect fuction

Hi!!..I know than prelude is based in the haskell module..but would you add the intersect fuction?...in f# I use sets..but javascript only has arrays...so...I think it would be an important add....

thanks!!!

Indices on each/map/filter

Both underscore.js and native javascript array forEach/map/filter/etc provide index values as the the second argument to the function. For example:

[1,3,5].map(function(d,i) { return [d,i] })
// [[1,0],[3,1],[5,2]]

It would be convenient if the prelude functions behaved the same way

installPrelude throws error in IE8

Installing prelude into the global namespace with prelude.installPrelude(window) fails in IE<=8.

This seems to be because IE versions <=8 don't allow overwriting properties on the window object. Because prelude includes a function called 'length', and error is throw when attempting to overwrite window.length. Incidentally, other JS environments' window have a .length too, but they don't mind if you overwrite it.

I got around this for my project by adding a function to prelude called 'safeInstall' which simply renames the 'length' function to 'len' while copying, if it detects that the destination object is window.

There are valid reasons to ignore this bug, for example, changing the function name breaks the isomorphism to the Haskell prelude library, diverges the API for other maintainers of the code, and you may or may not care about supporting IE8. These things don't happen to be a problem for me personally, but might prevent you incorporating a fix into the master. I just thought I'd bring this to your attention - maybe it's worth noting in the docs at least.

Thanks
L

preludels.com example is incorrect

foldr1 (-), [1 2 3 4 9] #=> 7

Wouldn't that be -1?

Also these two are wrong:

foldr (-), 9, [1 2 3 4]       #=> 7
foldr (+), 'e', <[ a b c d ]> #=> 'abcde'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.