Live data from Hacker News

A spreadsheet in fewer than 30 lines of JavaScript, no library used

jsfiddle.net

231–240 of 274 posts

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#231
post #222
post #93

It's not really comparable, but minimalistic spreadsheet applications always remind me of Dan Piponi's Haskell `loeb` function [0]---a "one-line" "spreadsheet" "implementation". The blog post is very interesting to read, but here's the meat. We're looking for a function with type loeb :: Functor f => f (f a -> a) -> f a and without thinking about the meaning of it, we can implement it as loeb x = fmap (\a -> a (loeb…

One more cool trick from the same article (slightly modified). It's a factorial function computed on an "infinite spreadsheet". But how? fact = loeb fact' where fact' 0 _ = 1 fact' n f = n*f (n-1) If we ask GHC for the type of `fact'` then we see that it is Int -> ((Int -> Int) -> Int) which we can interpret via the `Reader Int` monad as being m (m Int -> Int) -- for m a = (Int -> a) Now, `f :: (Int -> a)` as a funct…

And because I just find this really fascinating, here's a more direct implementation of `fact`

    data Stream a = Stream a (Stream a)
                    deriving Functor

    tabulate :: (Int -> a) -> Stream a
    tabulate f = go 0 where
      go n = Stream (f n) (go (succ n))

    index :: Stream a -> (Int -> a)
    index (Stream a _)  0 = a
    index (Stream a st) n = index st (pred n)

    -- btw: tabulate . index == id
    -- and  index . tabulate == id

    fact :: Int -> Int
    fact n = index (loeb facts) n where
      facts :: Stream (Stream Int -> Int)
      facts = tabulate $ \i stream -> i * index stream (i-1)

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#233
post #62

Earlier quoted context omitted.

And 80% of perceived functionality is what most users — even people who rely on spreadsheets for their work — need. I'm not saying including the other 20% of functionality isn't a good thing to do, but when you're creating a new product or open source project, you can get by (and thrive) building only the 20% of functionality that's used 80% of the time. Why? Because 1% of functionality is still better than 0% (never…

You missed his point. It doesn't actually work well. It doesn't do basic stuff, which makes it entirely unusable. Like you can't move up/down/left/right using the arrow keys. You can't save it. It crashes if you make basic mistakes. The 80% of perceived functionality is that it looks like a spreadsheet app and actually does some basic calculations. It actually does about 5% of what you'd expect even the most basic of…

Agree with your last point totally.

But 30 lines of code, what do you expect? Now imagine jQuery type library with a community of developers and you get... Google Docs :-p

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#235

Earlier quoted context omitted.

Doing this all in a webworker might be one way to get security - by sandboxing the with/eval?

Possibly. Web workers are isolated from the DOM, but can still do stuff like import other scripts or do XHR requests, but those would be limited to the same origin. It seems like that would be somewhat XSS safe since you are just passing strings back and forth. I really like this idea. evalSafeAsync(code,context,callback) That being said, for a spreadsheet, you need to bite the bullet and parse the formulas. I don't…

Script evaluated in a web worker would still have access to your application's cookies and would be able to interact with the server with the user's credentials. You probably don't want that.

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#236
post #191

Earlier quoted context omitted.

Just goes to show how much the far left and the far right has influence over our society

Eh, it's pretty easy to get things not on the far side of the screen; it's just hard to get it precisely in the center.

He was being punny...

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#240
post #203

Earlier quoted context omitted.

Consider ES6 too. It also does a good job at removing some Javascript quirks and adds sensible looping constructs, list comprehensions, generators and generator comprehensions, proxies, rest/spread arguments (removing the need for the magical `arguments` identifier), classes (just sugar for what we already wire up with prototypical inheritance), modules, fat arrow functions etc. No help on the DSL front though - stil…

I like the way ES6 is going, but Coffeescript has the advantage of maintaining compatibility with old (like, old-old) browsers. I hate having to do it, but you can't just ignore them. Server-side, sure, use the latest build you can get.

There is traceur (https://github.com/google/traceur-compiler) and it can provide most of the features - except proxies.
Post reply on HN