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
Corla Plankun
May 8, 2007

improve the lives of everyone
list comprehensions are really confusing to look at but once you get them they are insanely useful

code:
   thingList = [(1,2), (2,2), (2, 1), (0,1)] 
   newSet = [ "_" + str(thing[1]) for thing in thingList if thing[0]==2] 
   newSet
=> ['_2', '_1']
the dumb string thing is just an example that you can do whatever to thing[1]

Adbot
ADBOT LOVES YOU

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Bloody posted:

it probably isn't hard in python but I do not know how to do things

I have a collection of tuples. I want to take the subset of tuples where item 1 is equal to 2 and then apply a function to item 2, yielding a new set
in c# I'd do something like
var result = collection.where(e => e[0] == 2).select(e => function(e[1])

how do I do that in python in a way that isn't gross I know it has map and filter and fold and whatever but like how do I use them good

result = [function(e[1]) for e in collection if e[0] == 2]

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

or if its a big collection it might be better to do
result = (function(e[1]) for e in collection if e[0] == 2)
which gives you a lazily evaluated generator instead of a strictly evaluated list

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
i really just do not like python

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

or if you wanna use all the filter and map stuff like you were thinking:
result = map(lambda e: function(e[1]), filter(lambda e: e[0] == 2, collection))

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

but i think if you want to make guido happy, the elegant pythonic way would be:
code:
result = []
for e in collection:
    if e[0] == 2:
        result += function(e[1])
which avoids any gross functional or declarative code. much clearer this way

Jerry Bindle
May 16, 2003
python is weird. i had to convery some numpy python code to java this past week and gosh was it weird. i think it was a method called "copyto" where the first parameter was an array destination, the second was an array source, and the third was a predicate that operated on yet another array. i couldn't figure out how to handle that third array in a way that i was happy with. i had to abandon nice java8 functional interfaces and iterate through the arrays like some kind of idiot.

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Barnyard Protein posted:

python is weird. i had to convery some numpy python code to java this past week and gosh was it weird. i think it was a method called "copyto" where the first parameter was an array destination, the second was an array source, and the third was a predicate that operated on yet another array. i couldn't figure out how to handle that third array in a way that i was happy with. i had to abandon nice java8 functional interfaces and iterate through the arrays like some kind of idiot.

you should use haskell until you figure out how to do anything with functional interfaces instead of iteration

Jerry Bindle
May 16, 2003

fart simpson posted:

you should use haskell until you figure out how to do anything with functional interfaces instead of iteration

i could have done it in a nice way with a scheme or racket, a smarter person maybe could have done it better with java8. but java8's functional interfaces don't allow for functions with more that two inputs.

Corla Plankun
May 8, 2007

improve the lives of everyone
its really easy to bang out things as you think them up and its modules are more dependable and air-gapped than ruby so i like it a lot

but i hardly ever write anything thats more than two hundred lines long so i dont ever have to deal with the serious problems with plangs

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

so just zip inputs together and unzip them as needed

e: re: barnyard protein

MeruFM
Jul 27, 2010

fart simpson posted:

or if its a big collection it might be better to do
result = (function(e[1]) for e in collection if e[0] == 2)
which gives you a lazily evaluated generator instead of a strictly evaluated list

This is what most people should be using
list comprehension is just the most common use case of mapfilter

gonadic io
Feb 16, 2011

>>=

Shaggar posted:

why would you use scala when you could just use java?

Lol no thanks

And as for groovy: gently caress no. Having to add a few extra type sigs isn't the end of the world and I like most of the rest of scala.

Jerry Bindle
May 16, 2003

fart simpson posted:

so just zip inputs together and unzip them as needed

e: re: barnyard protein

i just googled that and yes okay you have convinced me to learn haskell, hot drat. i wonder what other stupid poo poo i've been doing to compensate for not knowing the right way to do things!

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Barnyard Protein posted:

i just googled that and yes okay you have convinced me to learn haskell, hot drat. i wonder what other stupid poo poo i've been doing to compensate for not knowing the right way to do things!

haskell functions only accept 1 argument. everything else is built upon that

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

Luigi Thirty posted:

No USER AIDS. I tried. :rip:



in sdl.c around line 213 there's a block:

code:

if (offset > MAX_BITMAP_OFFSET) {
...
}

which should actually be:

code:

if (offset >= MAX_BITMAP_OFFSET) {
...
}

tweaking that was enough to boot to the full Lisp environment on my Linux box

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

Luigi Thirty posted:

:eyepop:


for some reason the arch was set to pentium3. I had to set it to x86-64 to get it to compile. I get to a diagnostic screen but when it tries to boot from the disk i just get a black screen.

I am only on the surface of Richard Stallman's monument to autism but I like how extensible it is

it's just rms's maintenance work on others' design and implementation, he didn't even come up with the "lisp as extension/implementation language for emacs" idea

even gnu emacs isn't original to rms, I gather it started as a hack on gosmacs and then he had to replace that code ("oops")

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

Ralith posted:

Doesn't CLIM still sort of work on modern Common Lisp implementations?

I think you can get CLIM with the commercial implementations

I've been able to make McCLIM work on Linux (but not OS X) under SBCL, its example listener is bare-bones but does actually have some of the Symbolics features (like clicking on any compatible object in the transcript to provide input to the command line)

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

Barnyard Protein posted:

but java8's functional interfaces don't allow for functions with more that two inputs.
lmao what why

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?
just use the Common Lisp LOOP macro, it can do anything

it's the zombo.com of iteration

gonadic io
Feb 16, 2011

>>=

NihilCredo posted:

lmao what why

https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

Soricidus
Oct 21, 2010
freedom-hating statist shill

Corla Plankun posted:

the itertools module's only valid use case is making stack overflow answers long enough to submit

itertools.product is good and useful

but I hate the way half the itertools documentation is taken up with providing source code to useful functions that you have to c&p to use because they didn't put them in the actual module, why the gently caress do people do that

Shaggar
Apr 26, 2006

MALE SHOEGAZE posted:

i really just do not like python

its a bad language

Elder Postsman
Aug 30, 2000


i used hot bot to search for "teens"

I like Python. but I am a bad programmer too sooooo

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

fart simpson posted:

but i think if you want to make guido happy, the elegant pythonic way would be:
code:
result = []
for e in collection:
    if e[0] == 2:
        result += function(e[1])
which avoids any gross functional or declarative code. much clearer this way

if you're going this path then use .append

Python code:
        result.append(function(e[1]))
instead of += because... oh god I don't even wanna tell why, it's just depressing believe me on this one

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

i want to know why. tell me

cinci zoo sniper
Mar 15, 2013




Elder Postsman posted:

I like Python. but I am a bad programmer too sooooo
i like python because i'm joke of a code monkey

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
also indices are gross

code:
result = []
for first, second in collection:
    if first == 2:
        result.append(function(second))
code:
result = [function(second)
          for first, second in collection
          if first == 2]
unpacking ftw

Symbolic Butt fucked around with this message at 14:52 on Oct 9, 2015

Bloody
Mar 3, 2013

pythons functional stuff looks yucky

cinci zoo sniper
Mar 15, 2013




fart simpson posted:

i want to know why. tell me
i just woke up so don't yell much please but c += b is c = c + b, and when your c is array but your b is not an iterable thing of same dimension, python just calls you a doofus and stops working

if you do .append, you put add b as last element of c

cinci zoo sniper fucked around with this message at 14:53 on Oct 9, 2015

jesus WEP
Oct 17, 2004


Bloody posted:

pythons functional stuff looks yucky
map/reduce/filter are not so bad but they're way less nice to read than linq

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

kalstrams posted:

i just woke up so don't yell much please but c += b is c = c + b, and when your c is array but your b is not an iterable thing of same dimension, python just calls you a doofus and stops working

if you do .append, you put add b as last element of c

oh yeah. i belong in this thread

e: so obviously the answer is to do result += [function(e[1])]

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

kalstrams posted:

i just woke up so don't yell much please but c += b is c = c + b, and when your c is array but your b is not an iterable thing of same dimension, python just calls you a doofus and stops working

if you do .append, you put add b as last element of c
oh yeah I wasn't thinking of this but yeah there's also this lol


there was a discussion about this += poo poo earlier on this thread I think, I'll search for it later but the gist is

1. it's dumb slow because :shrek:
2. += calls .__iadd__ so sometimes a += b is NOT equivalent to a = a + b and this can bite your rear end
it's like a whole class of potential bugs exclusive to python because of this gotcha and variations of it. go with .append and .extend

basically only use + for number things, for everything else it's probably bad

Shaggar
Apr 26, 2006
lol python is bad

Luigi Thirty
Apr 30, 2006

Emergency confection port.

eschaton posted:

it's just rms's maintenance work on others' design and implementation, he didn't even come up with the "lisp as extension/implementation language for emacs" idea

even gnu emacs isn't original to rms, I gather it started as a hack on gosmacs and then he had to replace that code ("oops")

I know but I can never pass up a good rms joke much like he can never pass up foot fungus

cinci zoo sniper
Mar 15, 2013




Shaggar posted:

lol python is bad
i'm glad i get to use python as an alternative, my field is full of reject developments founded on inability to decide if fortran is cooler than c or no

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
python is mostly good

also numpy has its own idioms so the better way to do that would probably be using an index mask instead of forcing comprehensions/itertools whatever pseudo-functional shenanigans. gotta go full vector programming

qntm
Jun 17, 2009
code:
>>> x = (["a", "b"], 1)
>>> x[0] += ["c"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> x
(['a', 'b', 'c'], 1)
(The correct approach is x[0].append("c"))

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

if tuples are immutable, why can you mutate the data inside them?

Adbot
ADBOT LOVES YOU

cinci zoo sniper
Mar 15, 2013




fart simpson posted:

if tuples are immutable, why can you mutate the data inside them?
e: reading is valuable skill i should gain

e2: uhh, looks like tuple contains reference to object, not objects, so you caan gently caress with objects but not with references?

cinci zoo sniper fucked around with this message at 17:04 on Oct 9, 2015

  • Locked thread