Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
comedyblissoption
Mar 15, 2006

https://github.com/robpike/filter
Simple apply/filter/reduce package.

rob pike posted:

I wanted to see how hard it was to implement this sort of thing in Go, with as nice an API as I could manage. It wasn't hard.

Having written it a couple of years ago, I haven't had occasion to use it once. Instead, I just use "for" loops.

You shouldn't use it either.

Adbot
ADBOT LOVES YOU

comedyblissoption
Mar 15, 2006

https://groups.google.com/d/topic/golang-nuts/RKymTuSCHS0/discussion

Sean Talts posted:

Hi everyone,

I'm wondering if there are plans or it would be useful to implement map and filter on top of native Go collections, or maps and slices at least. Is there a reason that was not included? I've read a bit about why generics has not been included on a language level and instead they have been special cased on collections, but I don't see that precluding type-safe (i.e. not just translating everything to interface{} and back) implementations of map and filter. On the contrary, it seems to my admittedly Go-newbie self that they would, in fact, solve most of the use cases for generics. Thoughts? Does this violate some fundamental Go design decision I haven't yet encountered?

Thanks in advance,
Sean

PS I tried searching for this on the mailing list but my search-fu is weak and terms like "map" and "filter" are not particularly googleable. If I've missed a thread, feel free to link it! Thanks again.

quote:

I'm reluctant to add increasingly special purpose builtin functions.
There is no obvious endpoint as we head down that road.

Ian

quote:

The modern programmer thinks a newline is a thousand times harder to
type than any other character. If instead you take a newline as only
one keystroke, which it is, the fact that your program might not fit
on one line is a bearable burden.

-rob

comedyblissoption
Mar 15, 2006

why go is not good

comedyblissoption
Mar 15, 2006

dont use go when better alternatives exist lol

comedyblissoption
Mar 15, 2006

pram posted:

why do procedural languages need more functional garbage
eventually you may tire of janitoring for loops

comedyblissoption
Mar 15, 2006

benefits of higher order functions over manually written for loops:
  • composable
  • easier to read
  • easier to write
  • less bugs and mistakes
  • reveal intent
  • less repetition
  • can be more performant than quickly thrown together for loops when using lazy evaluation
  • can provide generalized ways to process generic traversable data structures

comedyblissoption
Mar 15, 2006

contrived C# code that i havent checked for compilation providing an example:

code:
int getSumOfFirstTenOddIntegersGreaterThanHundred(IEnumerable<int> ints) {
  return ints
    .Where(x => x > 100)
    .Where(IsOdd)
    .Take(10)
    .Sum();
}
this works on any ienumerable which is a generalized interface for enumerating over a data structure

unfortunately C# sucks on generalizing over all primitive numbers, but there are other languages that can do that

comedyblissoption
Mar 15, 2006

equivalent janitored for loop that i havent compiled because im bored:
code:
int getSumOfFirstTenOddIntegersGreaterThanHundred(IEnumerable<int> ints) {
  var sum = 0;
  var count = 0;
  foreach (var x in ints) {
    if (x > 100 && IsOdd(x)) {
      sum += x;
      count++;
      if (count == 10) {
        break;
      }
    }
  }

  return sum;
}
even for a ridiculously contrived simple example, it's much harder to detect if there may be an error in the program when writing and reading it

edit: lol i forgot the sum part of it because janitoring for loops is dumb

comedyblissoption fucked around with this message at 03:59 on May 23, 2015

comedyblissoption
Mar 15, 2006

java, javascript, c#, and python all have this "functional poo poo" and are as mainstream as it gets

idk if php supports it

comedyblissoption
Mar 15, 2006

for loops are minimalist

but i would argue that branch jump is more minimalist and therefore should be more preferred

comedyblissoption
Mar 15, 2006

my goal here is mainly to educate the benefits of using alternatives to for loops

i'm mostly tired of having to reverse engineer intent from looking at the code instead of an alternative of the code declaratively telling me its intent

comedyblissoption
Mar 15, 2006

at the very least we can warn the innocent away from go

comedyblissoption
Mar 15, 2006

Fanged Lawn Wormy posted:

thanks that's reassuring.

I initially wanted to write this function w/ structs, but got intimidated and backed out. I think I'm going to re-try it now with structs to learn that part, and see where it rolls from there
if you're trying to fade from one colour to another over time, that sounds like linear interpolation over time
http://stackoverflow.com/questions/13488957/interpolate-from-one-color-to-another

pass in the initial time, elapsed time, and fade times as parameters to determine how much interpolation there should be between an initial colour and end colour. then use this decided interpolation to return the interpolated colour.

your program will be more composable if you have separate functions for:
  • linearly interpolating between two colours
  • a function for linearly interpolating between two colours over given time parameters calling the above
  • functions that need to get the current colour for some ui component by calling the above function

a recursive function for colour interpolation over time sounds like a bad idea

encapsulating these colour interpolation functions so they don't reference global or static variables will make them re-usable for multiple ui components

comedyblissoption fucked around with this message at 14:49 on Jun 7, 2015

comedyblissoption
Mar 15, 2006

gonadic io posted:

jesus loving christ thanks microsoft for convincing me that i was going insane: http://stackoverflow.com/questions/5918534/why-cant-i-add-a-subfolder-in-a-f-project
F#, a programming language with worse file and function definition janitoring than C

comedyblissoption
Mar 15, 2006

if you search online more about this F# limitation, youll find a bunch of stockholmed programmers defending it as making your code better because this is obviously the only way to prevent cycles in your definitions

comedyblissoption
Mar 15, 2006

qntm posted:

I ask you, is there a worse code smell than people using "theValue" and "theStatus" for variable names
theStatus is helpful to differentiate the status in global scope as opposed to aStatus which is just in local function scope

comedyblissoption
Mar 15, 2006

that doesnt help much when you need to print out your code

comedyblissoption
Mar 15, 2006

coloured ink is worth more than my blood by volume

comedyblissoption
Mar 15, 2006

camel case is such idiot garbage prized by those who value screen real estate over being able to loving read anything

im the idiot in the codebase who randomly omits vowels on identifiers for no loving reason

comedyblissoption
Mar 15, 2006

xs and ys for collections are fine too

btn and pn for button and pane are not fine

comedyblissoption
Mar 15, 2006

cnt

comedyblissoption
Mar 15, 2006

Valeyard posted:

This reminds me of another thing I hate, when the same variable is used multiple times in the same scope but for different reasons. Like at some point in the logic that value isn't needed anymore, so next time we need a new variable let's just use that old onwt
mutability by default is the source of a lot of bugs

seeing this also usually means the function is way too loving big

function size too big should be a configureable compiler warning for all compilers

comedyblissoption fucked around with this message at 04:45 on Jun 10, 2015

comedyblissoption
Mar 15, 2006

gonadic io posted:

Turns out that the VS extension f# power tools will do this (although the feature is not turned on by default??) . It's a VS limitation, not a f# one.
Having folders doesn't matter. F# still forces you to strictly janitor the order of your module executions so that definitions precede use across the entire project. This is to the point that you have to ensure your main function is the last thing declared.

comedyblissoption
Mar 15, 2006

VikingofRock posted:

My biggest problem with python is that "fail fast" is not one of their design goals. You don't have a compiler to catch anything but the most basic syntax errors, you don't have a type system which keeps you from doing stupid stuff, and a bunch of stuff that should throw an exception doesn't.

For example, recently I was doing a thing where I was reading some data from a file, and then cutting out all the entries with some_field < 20. Except I forgot to cast some_field to numerical type. So later in some unrelated function, when I cut out everything where (some_field < 20), I ended up keeping everything because strings are always greater than numbers. So later in some unrelated function, things failed because I was passing it a bunch of junk data (because the things with some_field < 20 were junk).

If I had been coding this in a language with a stronger type system, it wouldn't have compiled because the return type of my parsing function would have been wrong. If I had been coding this in a language which threw exceptions for things like comparing '21' with 20, I would have been able to figure things out quickly there. But instead my small mistake in my parsing function broke things way, way later and I wasted a bunch of time trying to figure out why the hell nothing was working.

Apparently they've made ('21' > 20) throw an exception in python 3, but everyone still uses python 2 so that doesn't really help.
the common stockholm syndrome response to this is that it doesn't matter because you should've been writing unit tests!

(lol)

comedyblissoption
Mar 15, 2006

duck typing is a repulsive abomination foisted as a feature

comedyblissoption
Mar 15, 2006

im honestly a little bit baffled how dynamically typed languages are actually getting more popular than statically typed langs

burn everything to the ground

comedyblissoption
Mar 15, 2006

fidel sarcastro posted:

it's a solution to a problem no one was having, since you should be coding to an interface anyway.
to be fair, it solves the problem of getting novices to work w/ toy programs w/ minimal knowledge

comedyblissoption
Mar 15, 2006

unfortunately such a decision can have awful effects once you move beyond a toy program

i think a lot of damage has been done in the computer industry based on showing trivial toy examples and people not asking how such an approach might scale in complexity

edit: where complexity here means program complexity

comedyblissoption
Mar 15, 2006

uncurable mlady posted:

so I have a discussion question for the thread

as a terrible programmer, what are some hot tips on how to get your head around a sprawling lovely poorly documented code base?

as background, I own a monstrosity of a asp.net mvc application and an associated windows service that is responsible for marshaling integration tests and a whole bunch of other poo poo. it was initially built three or four years ago by some interns and no one who's currently with the company was instrumental in making it (and the people who were there for it weren't involved in making it so there's not a lot of institutional knowledge).

I've been trying to burn through the backlog of tickets and also fix + add new stuff where possible, but it's a loving mess. data stuff is split between EF or nhibernate seemingly at random, there's almost no comments (useful or otherwise), there appear to be multiple execution paths to the same outcome depending on what part of the UI you start a task from with no rhyme or reason, and it seems that there's dead code everywhere.

other than "attach debugger, set break points, spend a few days stepping through everything" what are some ways to attack the problem?
if the project seems ultimately un-rewarding career wise, i would put in minimum effort to fix things and try to get on another project or find another job

Otherwise, I would suggest heavily against rewriting it.

I would incrementally refactor and write unit tests as you do fixes and add features. You should prioritize your time on refactoring (by this I mean don't go off refactoring something that doesn't need fixing or is a rarely used component of the system or works just fine and doesn't need to be touched).

Identify sub-systems/modules/whatever of functionality. Refactor by using unit tests.

The unit tests should test the input and output of an entire module. Ideally you would write the unit tests and get them to pass w/ minimal refactoring before you start refactoring. You will probably have to reverse engineer the code to come up with unit tests and also send in data into the current system and use the debugger to figure out the outputs. This is very time consuming. You should refactor in small incremental steps and make sure all the tests pass ever time and put these commits in source control. Not taking small incremental steps can potentially put you in a special hell.

You could alternatively YOLO it and ride by the seat of your pants and refactor a bunch of poo poo all at once without tests and you might somehow not have broken something. This might be faster if you're lucky.

Be very careful of deleting or changing lines of code that you don't fully understand the reason for their existence.

AFAIK, businesses will not reward you and will criticize you for trying to improve the maintainability of applications in maintenance mode. They just want you to shovel through a bunch of poo poo and ask you to do the impossible of not breaking things in an unmaintainable hell. Every single refactoring is time consuming and carries the risk of causing bugs that didn't exist before. Economically, you'd need to weigh if improved long-term maintainability outweighs these risks. A business and manager will almost always be short sighted and choose the shoot yourself in the foot method.

comedyblissoption fucked around with this message at 05:56 on Jun 11, 2015

comedyblissoption
Mar 15, 2006

Barnyard Protein posted:

whats a good resource to learn how to architect an application The Good Way? like a case study, or a good example of a well designed open source java application that i could study? its frustrating, it feels like being competent at hammering nails into a board, but not having any clue about how to build a house.
Designing applications well is managing complexity. You should heavily favor approaches that make complexity manageable.

Pithy and very important ways to accomplish this:
  • Minimize side effects and state
  • Try to model things as pure input/output computations
  • Make your functions and modules small

Knowing this is a lot more important than some design patterns bullshit.

Learning a functional programming lang will probably make you a better Java programmer than reading some random Java book.

This is all anathema to popular OOP practices and sensibilities though even though you could do these things in Java.

comedyblissoption
Mar 15, 2006

the only good thing about go is it lets you immediately realize when someone has terrible opinions about programming languages

comedyblissoption
Mar 15, 2006

only nerds in their ivory towers would care about making their own container data types with static type safety

if you can't just rely on the static type safety of the built in container types, you're doing something wrong and need to rethink how you're going about the problem

nobody would ever need a different hashtable than the built in golang one

that's just reinventing the wheel

comedyblissoption
Mar 15, 2006

golang is for programmers who get poo poo done and dont jack off in their ivory towers about type systems

comedyblissoption
Mar 15, 2006

interface{} is just like duck typing and you dont see people complain about that in python

comedyblissoption
Mar 15, 2006

rob pike is stealing the blood and sweat and tears of a great amount of naive programmers lol

comedyblissoption
Mar 15, 2006

i forgot about poe's law

there are actually people that defend the lack of generics in go

http://andlabs.lostsig.com/blog/2014/12/09/189/thoughts-on-generics
http://blog.jonathanoliver.com/golang-has-generics/
https://splice.com/blog/golang-improved-simplicity-reduced-maintenance/

comedyblissoption
Mar 15, 2006

i wasnt even trying to troll i was being sarcastic immediately after i said golang is poo poo

golang is so bad that you can say something sarcastic right after you bash golang and people will think youre being serious b/c this is what golangers actually believe

comedyblissoption
Mar 15, 2006

the funniest part is that golang will still be a bad language even if they inevitably add in decent generics

but it'd still be over 8 years since the language's public inception in which every single year involved everyone that wasn't blind to the development of programming languages for the past 5 decades asking for generics

comedyblissoption
Mar 15, 2006

i agree

i'm mad mainly at rob pike, google, et al. for hyping a bad language for naive programmers trying to break out of the hellhole of dynamic typing that is ruby or python or w/e

i think the most we can do is just educate people away from voluntarily choosing the language

Adbot
ADBOT LOVES YOU

comedyblissoption
Mar 15, 2006

i actually prefer golang's approach of functional composition over java/c# where everything has OOP kool-aid boilerplate for no good reason

i also like that you can't do subtype polymorphism and must use interfaces

i think moving away from the OOP madness is part of the appeal of golang

but it's not compelling when you replace OOP madness with another form of madness that is actually worse

  • Locked thread