this post was submitted on 23 Aug 2023
71 points (94.9% liked)

Programming

17028 readers
83 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS
 

The title would probably be confusing, but I could not make it better than this. I noticed that most programming languages are limited to the alphanumerical set along with the special characters present in a general keyboard. I wondered if this posed a barrier for developers on what characters they were limited to program in, or if it was intentional from the start that these keys would be the most optimal characters for a program to be coded in by a human and was later adopted as a standard for every user. Basically, are the modern keyboards built around programming languages or are programming languages built around these keyboards?

you are viewing a single comment's thread
view the rest of the comments
[–] fubo@lemmy.world 5 points 1 year ago (1 children)

The core of that Life expression works by taking the array of cells and shifting it in the eight different directions, then summing those arrays to get the population counts.

I tried translating this into Python — but I've never written numpy code before, so this is probably less efficient than it could be. But it does work and you can see a glider move through a few generations.

The array-shifting logic is in the populations function, with np.roll being the equivalent of the APL rotate operation (written as and in the original).

import numpy as np

def alive(popu, cell):
    return int(popu == 3 or (popu == 4 and cell))
alive = np.frompyfunc(alive, 2, 1)

def populations(grid):
    return np.array(
            [ np.roll(r1, shift, 1)
                for shift in [-1, 0, 1]
                for r1 in (np.roll(grid, shift, 0) for shift in [-1, 0, 1]) ]
            ).sum(axis=0)

def nextgen(grid):
    return alive(populations(grid), grid)

grid = np.array([[0, 0, 0, 0, 0, 0],
                 [0, 0, 1, 0, 0, 0],
                 [0, 0, 0, 1, 0, 0],
                 [0, 1, 1, 1, 0, 0],
                 [0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0]])

for gens in range(5):
    print(grid)
    grid = nextgen(grid)
print(grid)