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
Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
reading this thread because I'm way behind the other one

hello there present yospos I'm going back to the past now

Adbot
ADBOT LOVES YOU

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
I would love to work remotely, like a really good job here in the 3rd world would be like $13k/year.

come on yospos hire me I must be super cheap

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
I'm not serious about hiring me though, I'm terrible

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
I never thought of javascript as a plang, I feel like it's on a category of its own: javascriptlangs

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
I wish I knew some basic number representation poo poo before I attempted to do anything in Fortran.



oh the pain, don't make this mistake fellow terrible programmers

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
my limited experience with ruby made me respect the language, it was much better than I ever expected. idk it's like ruby got oop right or something at least compared to python, perl or... php.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
also I like to think that sulk is a dumbass and doesn't know poo poo

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

prefect posted:

can you give me a thumbnail sketch of how it got oop right and the others got it wrong?

(not being a snarky prick; genuinely curious, because something about ruby gives me the heebie-jeebies and/or the willies)

uh no, I can't? sorry I'm a terrible programmer. I guess it boils down a lot to the syntax, in ruby oop feels much more at home compared to say python. here's my sorry excuse for an example:

Ruby code:
class Pair
    attr_accessor :x, :y
    def initialize(x, y)
        @x = x
        @y = y
    end
    def norm
        Math.sqrt(x*x + y*y)
    end
end

class PolarPair < Pair
    attr_accessor :r, :theta
    def initialize(r, theta)
        @r = r
        @theta = theta
    end
    def x
        @r * Math.cos(@theta)
    end
    def y
        @r * Math.sin(@theta)
    end
    def x= a
        b = y
        @theta = Math.atan2(b, a)
        @r = Math.sqrt(a*a + b*b)
        self
    end
    def y= b
        a = y
        @theta = Math.atan2(b, a)
        @r = Math.sqrt(a*a + b*b)
        self
    end
end
Python code:
import math

class Pair(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def norm(self):
        return math.sqrt(self.x*self.x + self.y*self.y)

class PolarPair(Pair):
    def __init__(self, r, theta):
        self.r = r
        self.theta = theta
    @property
    def x(self):
        return self.r * math.cos(self.theta)
    @property
    def y(self):
        return self.r * math.sin(self.theta)
    @x.setter
    def x(self, a):
        b = y
        self.theta = math.atan2(b, a)
        self.r = math.sqrt(a*a + b*b)
        return self
    @y.setter
    def y(self, b):
        a = y
        self.theta = math.atan2(b, a)
        self.r = math.sqrt(a*a + b*b)
        return self
also I loved messing around with the comparable/enumerable mixins poo poo. but yeah, I'm a suspicious person to say anything, I rarely done any non-trivial oop

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

gucci void main posted:

ruby is bad not as much because it's slow and is mostly used for web poo poo, but because of the way that a majority of ruby programmers write their code. also there are way too many tdd zealots who are worse than any javascript obsessor

oh ok, that sounds fair enough

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

chumpchous posted:

numbers as objects is cool, it lets you do things like
code:
class Numeric
   def prime?
      #find if self is prime, return bool
   end
end

3.prime?
=> true
of course overriding base classes is a terrible idea if you're working on a large codebase but it makes personal projects like project euler a lot of fun

lol most definitely. the mixins I wrote overriding Numeric was some fun poo poo

loving around p-langs is bliss

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
the Unix environment is my IDE

I'm a wizard bitches

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
I use vim and also I hate myself for using it and not bothering to make any effort to try something else.

tbf it doesn't matter really

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
I think vim is more like a drug, once an addict you'll never be clean so kids don't do vim

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

polpotpi posted:

vim doesn't but everyone who uses vim fills it with garbage plugins to mimic things that are naturally part of a GUI IDE

I don't do that but I know what you're saying, it's like this silly guy here: https://www.youtube.com/watch?v=YhqsjUUHj6g

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

FamDav posted:

i skimmed but he basically set up a vimrc and has a couple of plugins.

guy doesnt even have fugitive/git-gutter on there

he seems too clueless about simple commands and he's already changing a lot of poo poo geeeeez

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

FamDav posted:

the syntax highlighting/indenting/stuff on
tabs to spaces so you dont gently caress up our bespoke codebase
comma leader
double-j for exiting insert mode
semicolon to colon

oh hey all those seem nice and cool I gue--

FamDav posted:

unmap arrows :getin:

:stare:

why

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

FamDav posted:

code:
      au BufEnter *.hs compiler ghc
      autocmd FileType ocaml source /Users/EddardFuckingStark/.opam/system/share/typerex/ocp-indent/ocp-indent.vim
be consistent, use au or autocmd, not both

and there's no reason whatsoever for using these short not self-explanatory 2 letters commands in a loving config file.


also I'm Eddard *loving* Stark

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
what if you ask for sublime and your boss pays for it and then you're an rear end and go like "you know what VIM IS BETTER i love my vimrc" and never use sublime

I'm just wondering if the boss would get pissed for this or if they just don't care.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
I used to have jj as esc but I never used it.


idk there's nothing wrong with punching esc I guess

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
notepad++ used to have some really bad syntax highlighting for python, did it get better?

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

uG posted:

all you python scrubs are going to be programming perl6 in 10 years (if you havent killed urself by then)

eh I'm ok with this. like it could be way worse.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
Python code:
a = [i.__add__ for i in xrange(5)]
I would've done this :smug:

No, I can't explain why lambda is such a piece of poo poo in python other than mumbling "lexical scope and closure or something". My rule of thumb is that if I want to do something fancy like I would in racket it'll go wrong in python.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
also there's functools.partial, it never disappointed me unlike loving lambda

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
oh hey the same thing already posted from 2 pages ago

JewKiller 3000 posted:

code:
>>> def python():
...   this_language = "su"
...   def broken(x):
...     this_language = this_language + x
...   return broken("cks")
... 
>>> python()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in python
  File "<stdin>", line 4, in broken
UnboundLocalError: local variable 'this_language' referenced before assignment



prefect posted:

could somebody explain why the first is okay but the second is bad? :confused:

the way I understand is when you do i += 1 inside a block it's like:

- oh hey is there some name assignment around here? yes. yes there is. gently caress the corresponding nonlocal plebs
- creates new name i
- tries to assign (i + 1) to i
- oops can't evaluate this expression coz there's nothing in i yet!

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

chumpchous posted:

Wow what the gently caress??

Does it work if you do i = i + 1?

no

also this returns the same error without any prints:

Python code:
i = 78
def foo():
    print(i)
    i += 1
I think this was the most surprising thing to me: the fact that it apparently looks around for name assignments before doing anything.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

PleasingFungus posted:

code:
a = [(lambda x: lambda y: x + y)(i) for i in xrange(5)]
or more verbosely

code:
def gen_func(x):
   def out_func(y):
       return x + y
a = [gen_func(i) for i in xrange(5)]
which reserves a separate scope to store the value ('i') you want for each generated function

I see what's happening in the case of the named function and when you try to use it but it still baffles me in the case of the lambda, it's like they aren't really anonymous or something.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

tef posted:

the i is stored in the outer scope, and not copied into the lambda, so i keeps the value from exiting the loop.

code:
a = []
for i in xrange(5):
	a.append(lambda x, i=i: i + x)

print a[1](1)
happy now ?

code:
a = []
for i in xrange(5):
    if i == 3: a.append(lambda x: i + x)

print a[0](1)  # 5
oh my goooooood

ok I'm happy now.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

tef posted:

ps should i make a smart dog book thread in CoC and then argue about stuff?
yes

prefect posted:

i think that looks pretty cool, but i am also fond of perl, so you might want to reconsider that code :D
the comprehension idiom is a strong thing in python (it's super fast) and in the case where it might have too many for loops and ifs you can just break it in different lines and it still looks very readable!

Python code:
    return next((WATER, ZEBRA)
                for (red, green, ivory, yellow, blue) in c(orderings)
                if imright(green, ivory)
                for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in c(orderings)
                if Englishman is red
                if Norwegian is first
                if nextto(Norwegian, blue)
                for (coffee, tea, milk, oj, WATER) in c(orderings)
                if coffee is green
                if Ukranian is tea
                if milk is middle
                for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments) in c(orderings)
                if Kools is yellow
                if LuckyStrike is oj
                if Japanese is Parliaments
                for (dog, snails, fox, horse, ZEBRA) in c(orderings)
                if Spaniard is dog
                if OldGold is snails
                if nextto(Chesterfields, fox)
                if nextto(Kools, horse)
                )
with that said I couldn't convince anyone irl of how great comprehensions are, even after I show this example from Peter Norvig's code. :(

everybody is exactly like "I don't need this perl poo poo in my python"

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
:smith: oh well

it looks good to me and understandable for those kinds of brute-force things and also very convenient to tinker and reorder the loops. it's not very natural at first but I guess I got used with the bottom-up flow from comprehensions? something huge like this rarely came up for me besides challenge problems though

but yeah I guess I gotta rethink this stuff

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
anyway here's the entire code:

Python code:
import itertools
import time

def imright(h1, h2):
    "House h1 is immediately right of h2 if h1-h2 == 1."
    return h1-h2 == 1

def nextto(h1, h2):
    "Two houses are next to each other if they differ by 1."
    return abs(h1-h2) == 1

def zebra_puzzle():
    "Return a tuple (WATER, ZEBRA indicating their house numbers."
    houses = first, _, middle, _, _ = [1, 2, 3, 4, 5]
    orderings = list(itertools.permutations(houses)) # 1
    return next((WATER, ZEBRA)
                for (red, green, ivory, yellow, blue) in c(orderings)
                if imright(green, ivory)
                for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in c(orderings)
                if Englishman is red
                if Norwegian is first
                if nextto(Norwegian, blue)
                for (coffee, tea, milk, oj, WATER) in c(orderings)
                if coffee is green
                if Ukranian is tea
                if milk is middle
                for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments) in c(orderings)
                if Kools is yellow
                if LuckyStrike is oj
                if Japanese is Parliaments
                for (dog, snails, fox, horse, ZEBRA) in c(orderings)
                if Spaniard is dog
                if OldGold is snails
                if nextto(Chesterfields, fox)
                if nextto(Kools, horse)
                )

def c(sequence):
    c.starts += 1
    for item in sequence:
        c.items += 1
        yield item

def instrument_fn(fn, *args):
    c.starts, c.items = 0, 0
    result = fn(*args)
    print('%s got %s with %5d iters over %7d items'%(
        fn.__name__, result, c.starts, c.items))
c and instrument_fn are just some temporary debug functions for counting the number of calls and doesn't really matter (it was just an excuse for him to introduce some new stuff like the unpacking notation)

here's one way to rewrite the function but it's pretty much the same thing qntm did and I didn't see his post before. :rolleyes:

Python code:
def zebra_puzzle():
    houses = first, _, middle, _, _ = [1, 2, 3, 4, 5]
    orderings = list(itertools.permutations(houses))
    for (red, green, ivory, yellow, blue) in orderings:
        if imright(green, ivory):
            for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in orderings:
                if Englishman is red and Norwegian is first and nextto(Norwegian, blue):
                    for (coffee, tea, milk, oj, WATER) in orderings:
                        if coffee is green and Ukranian is tea and milk is middle:
                            for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments) in orderings:
                                if Kools is yellow and LuckyStrike is oj and Japanese is Parliaments:
                                    for (dog, snails, fox, horse, ZEBRA) in orderings:
                                        if Spaniard is dog and OldGold is snails and nextto(Chesterfields, fox) and nextto(Kools, horse):
                                            return (WATER, ZEBRA)
geez, so much indentation.

I don't think in this case the comprehension would be faster because he's only getting the first possible solution but even so it looks better to me.



a similar brute force case where it would matter is in this problem. here's one of my first attempts before figuring the real trick:

Python code:
from itertools import combinations

COUNT_MODULO = 1000000009

def count(seq, bound):
    lseq = len(seq)
    return len({hash(subseq[:i] + (new_element, ) + subseq[i:])
                for subseq in set(combinations(seq, lseq-1))
                for i in xrange(lseq)
                for new_element in xrange(1, bound+1)}) % COUNT_MODULO
using the set comprehension here is considerably faster than going with something like
Python code:
def count(seq, bound):
    lseq = len(seq)
    s = set()
    for subseq in set(combinations(seq, lseq-1)):
        for i in xrange(lseq):
            for new_element in xrange(1, bound+1):
                s.add(hash(subseq[:i] + (new_element,) + subseq[i:]))
    return len(s) % COUNT_MODULO
but I guess from now on I'll stick with not using those "impossible to write in a 79 characters line" comprehensions because it's obfuscating the code and being a kind of premature optimization. it's just that I'm so used to it.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
oh hey that for-if ladder actually looks kinda cool when you put it this way

*farts*

I wonder if the zebra puzzle is one of those things that can be done particularly well in prolog from what I hear people say about it

edit: what the gently caress FamDav

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
reading sicp and it's really neat the way it explains tail recursion separating the idea of recursive procedures from recursive processes. also loving all the numerical examples. those are my terrible programmer's first impressions on this book thanks for reading.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
how fitting for js people to be douchebags

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
I think I use way too much breaks and continues in my code.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
idk it may be a symptom of my inability to decompose poo poo in small and smart looking functions





but using gotos not even once

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Mr SuperAwesome posted:

people still teach fortran in academia in tyool 13

don't diss the 'tran

Notorious b.s.d. posted:

numpy and scipy means we're stuck with python for the long haul regardless, because it will survive in the darkness of academic labs long after the outside world has abandoned it

look on the bright side: most of the academic types using python are replacing their fortran and ksh

I never seen ksh and I guess I'm glad for this because the bash scripts were already hideous enough

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Notorious b.s.d. posted:

tbh i don't even like operators that much for numbers.

i would be perfectly happy with a language that had explicit method names even for math

this is an angle where lisp syntax rules imo

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
code:
irb(main):001:0> 1/2+3
=> 3
irb(main):002:0> 1./2.+3
=> 0
:3:

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
this is why python numpy is a gift from gods, matlab is so drat awkward

Adbot
ADBOT LOVES YOU

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
Pollyana if you're already comfortable with the basic python stuff (lists, tuples, dictionaries, opening a file idk) take a look at this course. Peter Norvig, the guy who teaches it, seems to be more of a functional programming kind of person and doesn't really like using classes regularly. imo this is the right approach with python.

if you're insecure about python basics then check the mentioned cs101, I never watched it but I hear it's good. if you're into reading books check Think Python. I first learned python with that and it's p good.

if you REALLY like oop, WHOA CLASSES AND STUFF then go learn ruby (if you don't care). but actually going with java would probably be the non-joke path to go but don't ask me about that, idk poo poo about java.

  • Locked thread