Open Side Menu Go to the Top
Register
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

05-03-2015 , 02:40 PM
omg, I found Suzzer's contact at Weapon Company X:

:-D
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-03-2015 , 06:38 PM
Quote:
Originally Posted by daveT
Someone just got dinged for creating O(n^3) because they nested a bunch of functional crap together. I really don't think it is good to start someone off with functional, or at the least, introduce a bunch of FP magic to someone who doesn't understand what is happening once that compiles.

Our computers are sick fast, we all have crazy fast internet, yet, the web is getting slower and slower by the day, unless you use plug-ins to stop this. I feel inclined at some point to begin a thread on why FP is outright dangerous, especially for JS-slingers with no foundations.

I know I have a reputation for being FP-first, but that's not the truth. The dangers of using FP was well-defined and the limitations weren't sugar-coated.
this is a fair enough warning. also, to clarify, i am not arguing that functional ways of doing things are always the best. my main point, as always, is to emphasize matching concepts in code with those that naturally exist in your head.

so if you are doing something to every element of a list, you shouldn't be a using a loop. you should be using map, because map is the reification in code of that concept.

Quote:
Originally Posted by greg nice
do you have some example problems that someone could use to play around with to learn these concepts? or perhaps a link to some?
hmmm... i don't know offhand of a set of problems specifically to drill you on map, reduce, etc (though i'm sure they exist), but if you do the first 5-10 problems on exercism.io, you'll find a number of natural opportunities to use them. another helpful exercise would be to look at your code, and whenever you see a "for" loop, stop and try to rewrite it using a functional method and see if it's more natural. for fixed sized loops, it almost always will be.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-03-2015 , 07:44 PM
I sort of see off-handed use of map / filter / reduce / etc akin to building a class and calling that object oriented programming. It may *look* like OO, but it isn't.

If people are really looking to see what all of these things mean, you can try out the requisite chapters of SICP, since the book requires you to build these items up. If you can't be bothered to learn enough lisp to do that, I could do a thread on this stuff. It is easy stuff, and I think it is somewhat enlightening as well.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-03-2015 , 07:51 PM
Quote:
Originally Posted by gaming_mouse

hmmm... i don't know offhand of a set of problems specifically to drill you on map, reduce, etc (though i'm sure they exist), but if you do the first 5-10 problems on exercism.io, you'll find a number of natural opportunities to use them. another helpful exercise would be to look at your code, and whenever you see a "for" loop, stop and try to rewrite it using a functional method and see if it's more natural. for fixed sized loops, it almost always will be.
perfect thanks
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-03-2015 , 08:13 PM
Quote:
Originally Posted by daveT
I sort of see off-handed use of map / filter / reduce / etc akin to building a class and calling that object oriented programming. It may *look* like OO, but it isn't.

If people are really looking to see what all of these things mean, you can try out the requisite chapters of SICP, since the book requires you to build these items up. If you can't be bothered to learn enough lisp to do that, I could do a thread on this stuff. It is easy stuff, and I think it is somewhat enlightening as well.
i'm always behind a rec for SICP, but you're missing my point if you think that intensive learning or textbook perusal is required to understand these concepts in the way i mean. i'm proposing making connections between these scary-sounding "functional programming" constructs and the you ordinarily think about the world.

"i want you to take all my clothes, clean them, and fold them"

Code:
map (fold . clean) my_clothes
we think naturally at this level of abstraction. we don't think of going through each item of clothing one by one, as with an index, and first cleaning, then folding it. we think primarily of the intended result (stacks of clean, folded clothes) and perhaps also of the initial state (jumbled pile of dirty clothes), but usually not at all of the specifics of the transformation. indeed, if you gave someone the instructions above, you wouldn't care at all how they accomplished the task -- if they did the cleaning and folding one item at a time, or in bulk.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-03-2015 , 10:40 PM
Quote:
Originally Posted by greg nice
do you have some example problems that someone could use to play around with to learn these concepts? or perhaps a link to some?
A good rule of thumb for map/reduce type problems is if you want to mutate every value in an array, use map. If you want to boil down an array to a filtered array, use reduce or filter. An example might be...

Code:
var array = [1, 5, 9, 10, 25, 39, 105]
_.map(array, function(ele){
  return ele + 5
})
We are just adding 5 to each number so it makes sense here to use map.

Code:
var array = [1, 5, 9, 10, 25, 39, 105]
_.reduce(array, function (memo, ele) {
  return ele >= 10 ? [].concat.call([], memo, ele) : memo
}, [])
Here we are asking if each number is greater than or equal to 10, add it to the array, otherwise return the previous value. It makes sense to use reduce here since we are *reducing* our original array to an array filtered via some criteria.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-03-2015 , 11:23 PM
What if I wanted to boil a 2D array down to a 1D array? Let's say every element in the new 1D array is the sum of a column (or alternatively row) in the original 2D array.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-03-2015 , 11:44 PM
Quote:
Originally Posted by suzzer99
What if I wanted to boil a 2D array down to a 1D array? Let's say every element in the new 1D array is the sum of a column (or alternatively row) in the original 2D array.
Code:
var array = [[1, 2, 3], [4, 5, 6]]
_.map(array, function (ele){
  return _.reduce(ele, function (memo, elem) {
    return +memo + elem
  }, '')
})
+memo + elem is shorthand for Number(memo) + elem. We need to make sure memo is a number. If we don't, the final array will be ['123', '456'] instead of the sum of each of the 2d arrays ([6, 15]).

I'm assuming colums in this case is the index of the array of array. For example array[0][1]. If we wanted to add rows it would probably be cleaner to rebuild the original array such that the columns and rows have been reversed. Then we just do the same thing as we did to reduce a 2d array down to a 1d array.

Last edited by Craggoo; 05-03-2015 at 11:54 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 01:13 AM
The reverse in case you're interested would be the following. If you didn't know how to do it, you can use the unzip underscore function to invert the 2d array.

Code:
var arr = [[1,2,3], [4,5,6], [7,8,9]]
var invertedArr = _.map(_.range(_.first(arr).length), function (ele) {
  return _.reduce(_.range(arr.length), function (memo, elem) {
    return +memo + arr[elem][ele]
  }, '')
})

Last edited by Craggoo; 05-04-2015 at 01:42 AM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 01:24 AM
Quote:
Originally Posted by suzzer99
What if I wanted to boil a 2D array down to a 1D array? Let's say every element in the new 1D array is the sum of a column (or alternatively row) in the original 2D array.
ruby one liner:

Code:
[[1, 2, 3], [4, 5, 6]].transpose.map{|x| x.reduce(:+)}
=> [5, 7, 9]
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 02:59 AM
Code:
=> (#(mapv + (%1 0) (%1 1)) [[1 2 3] [4 5 6]])
[5 7 9]
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 08:39 AM
nice!
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 09:28 AM
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 10:12 AM
Thanks Craggo. Cool way of forcing a number. I usually just multiply by 1. But + is cleaner.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 02:44 PM
Quote:
Originally Posted by greg nice
do you have some example problems that someone could use to play around with to learn these concepts? or perhaps a link to some?
I don't have a problem set but you can do any of the JS ones and should see ample of spots to use them (myArray.filter/map/reduce).
Conceptually the "big three" are rather simple:
filter=pass stuff through only if true, toss everything else
map=apply some operation to all elements
reduce=turn the collection into a single value (typical example being adding all values of a collection to get a total)

> var x = [1,2,3,4];
undefined
> x
[ 1, 2, 3, 4 ]
> x.filter(function(e){return e%2==0});
[ 2, 4 ]
> x.map(function(e){return e*2;});
[ 2, 4, 6, 8 ]
> x.reduce(function(e,e2){return e+e2;});
10

So yeah easiest example is map in place of array iteration with a for loop.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 02:47 PM
Quote:
Originally Posted by Somnius
I have heard that it is good practice to code in a way that allows a coworker to look at it and understand your objective as quickly as possible.
Don't aim so high. It's a good first step if you can read your own code after a couple of weeks (no this is not a snide remark or joke). so the first step should be to structure your code in a way that makes perfect sense to yourself.

Quote:
Originally Posted by candybar
I wouldn't waste much time trying to figure out a better way to solve this problem - just move onto harder problems.
I'm not sure I agree with this 100% of the time. There's value in perfecting mundane things and striving for that as a general mindset. Maybe I've spent too much time with Japanese coworkers* but when I'm at my best** I won't move on until I see no further improvements for now. Let me keep on dreaming about sushi
Interestingly it's also opposed to the general mantra of avoiding premature optimization if you take that mantra out of the algorithmic context.

*interesting aside: one of them would write a test, write some code, rinse repeat until he had a working version that covered the functional requirements (standard TDD) but then...delete everything and redo it. He used to say "before I send code to my review partner (for the commit) I rewrite it from scratch".
Strange might seem inefficient but he wrote very elegant code imo (often mindblowing yet simple). He told me that's how he always does it and sometimes he starts over a second time (also sometimes at different spots). He also did katas every morning which I hadn't seen before.
Needless to say he did everything tons faster than me despite the rewriting madness.
**rarely obviously

Quote:
Originally Posted by suzzer99
Quick survey: does anyone here actively program in Swing or any other Java client interfaces?
Intro classes here use javafx. Which is already strange to me because...mobile/WWW-world yo.

Last edited by clowntable; 05-04-2015 at 03:07 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 03:19 PM
http://lwn.net/SubscriberLink/641779/474137b50693725a/

Quote:
Programmers like to think they work in a field that is logical and analytical, but the truth is that there is no way to even talk about programming ability in a systematic way. ...we say that people "suck at programming" or that they "rock at programming", without leaving any room for those in between. Everyone is either an amazing programmer or "a worthless use of a seat".

But that would mean that programming skill is somehow distributed on a U-shaped curve. Most people are at one end or the other, which doesn't make much sense. Presumably, people learn throughout their careers, so how would they go from absolutely terrible to wonderful without traversing the middle ground?
From the guy misattributed with creating django

Thoughts? Feels?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 03:36 PM
Totally. There are all kinds of programmers all over the spectrum imo. However there have been studies, and this goes along with my general feeling - that a rock star programmer is like 40 times more productive than an average programmer. Also a truly terrible programmer is going to create productivity drains on every one around them.

So while the distribution may be more flat than a U-shaped curve, the productivity chart might look something like this:

** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 03:49 PM
Oh and:
Code:
> [[1,2,3], [4,5,6], [7,8,9]].map(function(e){ return e.reduce(function(x1,x2){return x1+x2;})});
[ 6, 15, 24 ]
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 05:46 PM
Quote:
Originally Posted by suzzer99
So while the distribution may be more flat than a U-shaped curve, the productivity chart might look something like this:

Fixed.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 08:35 PM
Lol Larry. Kind of true in my experience. A very talented co-worker spent almost a week writing this module to make something simple even easier. It's been used twice since saving a whopping 8 lines of code
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 10:05 PM
Quote:
Originally Posted by suzzer99
Totally. There are all kinds of programmers all over the spectrum imo. However there have been studies, and this goes along with my general feeling - that a rock star programmer is like 40 times more productive than an average programmer. Also a truly terrible programmer is going to create productivity drains on every one around them.

So while the distribution may be more flat than a U-shaped curve, the productivity chart might look something like this:

A cite of the studies would be interesting to find out how productivity was measured.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 10:05 PM
Quote:
Originally Posted by blackize5
Lol Larry. Kind of true in my experience. A very talented co-worker spent almost a week writing this module to make something simple even easier. It's been used twice since saving a whopping 8 lines of code
True rock stars know where and when to apply their talents for the best leverage. A super-talented programmer who wastes time on white whale vanity projects isn't a rock star any more than a poker player who can make great reads but does other dumb stuff is a great poker player.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 10:08 PM
Quote:
Originally Posted by adios
A cite of the studies would be interesting to find out how productivity was measured.
It's in some book I read that actually said "up to 38 times more productive" - because 40 times sounded too much like a number they just pulled out of their ass I'm guessing. Lol at measuring programmer productivity to that level of precision.

But anyway I do feel like just in making good decisions up front and steering away from roads that lead to trouble - a really great programmer saves a **** ton of productivity over the life-cycle of the product vs. an average programmer.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-04-2015 , 11:27 PM
Quote:
Originally Posted by gaming_mouse
nice!
Glad you like it, and surprised you can read it.

I think of map as interleaving. I think the Ruby does this, but I'm not sure why you didn't do this directly.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote

      
m