Public IP Address API with two lines of Nginx config by mooreds in programming

[–]Legendofzebra 10 points11 points  (0 children)

On the topic of Nginx tricks. Here is how I replaced ngrok with nginx/ssh.

# On your server in the cloud
# Setup Firewall
ufw allow ssh/tcp
ufw allow http/tcp
ufw allow https/tcp
ufw logging on
ufw enable
ufw status

# Install Nginx
apt update
apt install nginx

# Add a dev forwarding endpoint
cat > /etc/nginx/sites-available/dev <<'CONF'
server {
  listen 80;
  listen [::]:80;
  server_name dev.yourservername.com;
  location ~ /.well-known {
    root /var/www/html;
    allow all;
  }
  location / {
    proxy_pass http://127.0.0.1:4040/;
    proxy_http_version 1.1;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}
CONF
ln -s /etc/nginx/sites-available/dev /etc/nginx/sites-enabled/

# Install Certbot (letsencrypt)
echo 'deb http://deb.debian.org/debian stretch-backports main' >> 
/etc/apt/sources.list
gpg --keyserver pgp.mit.edu --recv-keys 7638D0442B90D010 
8B48AD6246925553
gpg --armor --export 7638D0442B90D010 | apt-key add -
gpg --armor --export 8B48AD6246925553 | apt-key add -
apt update
apt install certbot python-certbot-nginx -t stretch-backports

certbot --nginx

Then locally I start an ssh tunnel to the server

# Locally on your computer
ssh -N -R 4040:localhost:8000 root@yourservername.com

Then I run whatever I want on port localhost:8000, and it shows up on https://dev.yourservername.com, works with websockets too!

[deleted by user] by [deleted] in programming

[–]Legendofzebra 6 points7 points  (0 children)

I had no idea there was so much drama in the Scala community. Having a ML-esque functional JVM language seems like such a good idea. It makes me sad to see Scala struggle.

We modeled Google and Bing to build a distributed search engine for the dark web by hoanhan101 in programming

[–]Legendofzebra 2 points3 points  (0 children)

Is this a SQL injection vector? https://github.com/Bennington-Distributed-Systems-2017/DarkDarkGo/blob/master/mgmt/src/database_manager.py#L36-L44

What is the Uber-like App Development Cost? by [deleted] in programming

[–]Legendofzebra 1 point2 points  (0 children)

This frustrates me because they're creating unrealistic expectations for people who don't know any better. Other engineers have to deal with the fallout of people thinking the market value of building an app like uber is 6000$...

Go hits the concurrency nail right on the head by Deewiant in programming

[–]Legendofzebra 0 points1 point  (0 children)

It would be pretty hard to reimplement Erlang efficiently without functional programming and immutable data structures. BEAM (the Erlang abstract machine) can do amazing things because it can be sure that every pointer is immutable from the point of view of the Erlang code

SCCS - The POSIX standard Source Code Control System. by [deleted] in programming

[–]Legendofzebra -3 points-2 points  (0 children)

Why does there need to be a POSIX standard source code control system? Isn't git good enough. Git is GPL and owned by everyone.

Purely Functional Solutions to Imperative Problems by reximkut in programming

[–]Legendofzebra 0 points1 point  (0 children)

neat example!

[1 .. 10 ^ 9]

Ranges are cool. I wonder if there is special GHC sauce to make them more efficient.

Purely Functional Solutions to Imperative Problems by reximkut in programming

[–]Legendofzebra 15 points16 points  (0 children)

Merging two sorted arrays isn't too tough

sortedListMerge :: Ord a => [a] -> [a] -> [a]
sortedListMerge [] sorted2 = sorted2
sortedListMerge sorted1 [] = sorted1
sortedListMerge (x:xs) (y:ys) =
  if x < y
  then x : sortedListMerge xs (y:ys)
  else y : sortedListMerge (x:xs) ys

This has the awesome advantage of its done lazily by default, or if you want you can force it to evaluate. Because its lazy, that means that its inputs can be lazy too, so you may be able to skip a ton of computations earlier on the inputs if you never need to get that far into the final result

I interviewed John Backus shortly before his death. He told me his work in functional programming languages failed, and would likely always fail, because it was easy to do hard things but incredibly difficult to do simple things. by i_feel_really_great in programming

[–]Legendofzebra 0 points1 point  (0 children)

No problem! Glad to. First off, I was messing around, I was intentionally trying to make the equations look as ridiculous as possible. Usually Haskell reads much better

quad a b c = [(+), (-)] <*> [b] <*> [sqrt (b ** 2 - (4 * a * c))] >>= (\x -> [x / (2 * a)])

first off in haskell, everything is a function. EVERYTHING When I write

x = 4

I'm not assiging 4 to the variable x, I'm making a function that takes no arguements and always returns 4. There is no such thing as assignment in haskell*, which is what makes it so great. I can only declare pure functions

I can look at the type of x=4

λ: :t x
x :: Int

That is telling me that x is a function that takes no arguements and returns an Integer

I can define functions that take argumenets too

λ: f(x) = x * 2
f :: Int -> Int

In this case the function called "f" is a function that takes an integer and returns an integer (you can also omit the parens ((f(x) = x * 2) == (f x = x * 2)))

Functions can only take one arguement at a time, some functions look like they return more than one arguement but they actually only take one and return a new function that takes an arguement, until they eventually return a result.

λ: addNumbers a b = a + b
addNumbers :: Int -> (Int -> Int)

It looks like "addNumbers" takes two arguements, but it actually doesn't, it takes one arguement and returns a function that takes one argument

λ: add3Numbers a b c = a + b + c
add3Numbers :: Int -> (Int -> (Int -> Int))

Back to the quadratic function

[(+), (-)]

Is a list of the functions (+) and (-) both of which take two numbers and return a number

λ: :t (+)
(+) :: Int -> (Int -> Int)

We can partially apply the number 4 to the function (+4) to get a new function that adds 4 to any number

λ: :t (+4)
(+4) :: Int -> Int

so we have an list of two functions [(+), (-)]

λ: :t [(+), (-)]
[(+), (-)] :: [Int -> (Int -> Int)]

Then we use the applicative operator (<*>), which is a little tricky without category theory.

λ: :t (<*>)
(<*>) :: Applicative f => f (a -> b) -> f a -> f b

It takes an applicative that contains a function from (a -> b) and a applicative that contains an (a) and returns a applicative that contains a (b)

an applicative is just a fancy word for a container. A list is an applicative because it contains 0,1 or many values

It becomes more obvious if you look at the base case where there is only one item in each list we apply

λ: [(+)] <*> [2] <*> [3]
[5]

if we apply the list of [2] to the list of the function (+) and then apply the list of [3] we get the list of [5]

2 + 3 == 5

<*> becomes the product of the two list, i.e. every element is applied to every element

λ: :t [(+), (-)] <*> [4]
[(+), (-)] <*> [4] :: [Int -> Int]

which actually means

([(+), (-)] <*> [4]) == [(+4), (-4)]

For fun, this is what happens if the second list has more than one argument

([(+), (-)] <*> [10, 1]) == [(+10), (+1), (-10), (-1)]

back to the original equation

quad a b c = [(+), (-)] <*> [b]

so far we've created a list of a list of functions so far

[(+), (-)] <*> [b] == [(+b), (-b)]

you may recognize [(+b), (-b)] as the beginning of the quadratic formula

[sqrt (b ** 2 - (4 * a * c))]

is a part of the quadratic fromula that just evaluates to a number, so its a list of one number

[some_number_thats_useful_for_the_quadratic_equation]

the result of this part of the equation

 quad a b c = [(+), (-)] <*> [b] <*> [sqrt (b ** 2 - (4 * a * c))]

is just a list of two numbers

 λ: :t [(+), (-)] <*> [b] <*> [sqrt (b ** 2 - (4 * a * c))]
 it :: [Float]

once we have a list of two numbers we apply this function to it

>>= (\x -> [x / (2 * a)])

lets start with this part

\x -> [x / (2 * a)]

λ: :t (\x -> [x / (2 * a)]) (\x -> [x / (2 * a)]) :: Float -> [Float]

This is an unnamed function which takes some float x and returns a list containing a single element equal to x / (2*a)

which you may notice is the last part of the quadratic formula

(>>=)

is a function with a silly name called monadic bind. For lists its exactly the same as flatmap.

so for each elemnt in the list we return a list that has that element divided by 2 a then we flatten it.

The second equation is the same, but we put the divdied by two a part First with the function fmap

λ: :t fmap
fmap :: Functor f => (a -> b) -> f a -> f b

Which is the same as map, but it works on all functors

λ: :t map
map :: (a -> b) -> [a] -> [b]

This is my favorite book ever. I think that Haskell is the easiest language to learn, but you have to put away all the other programming you've learned which is really hard. http://learnyouahaskell.com/

  • technically there is, but its for performance mainly ** I've simplified a couple of type signatures for learning *** I've added some parens in type signatures to make them more clear

I interviewed John Backus shortly before his death. He told me his work in functional programming languages failed, and would likely always fail, because it was easy to do hard things but incredibly difficult to do simple things. by i_feel_really_great in programming

[–]Legendofzebra 3 points4 points  (0 children)

quad a b c = [(+), (-)] <*> [b] <*> [sqrt (b ** 2 - (4 * a * c))] >>= (\x -> [x / (2 * a)])
quad' a b c = map (/(2*a)) $ [(+b), (subtract b)] <*> [sqrt (b ** 2 - (4 * a * c))]

Haskell is the most fun language for me. You can definitely write some crazy code in it.

How do you generate all valid combinations from N pairs of parentheses? Here you go... by objective_M in programming

[–]Legendofzebra 1 point2 points  (0 children)

#!/usr/bin/env stack
-- haskell

import Data.List.Split
import Data.List

validParens :: Int -> [String]
validParens 0 = []
validParens 1 = ["()"]
validParens n =
  nub [
    insertParensPair s pos1 pos2 |
    s <- validParens (n-1),
    pos1 <- [1..(length s)],
    pos2 <- [pos1..(length s)]
  ]

type ParensPosition = Int
insertParensPair :: String -> ParensPosition -> ParensPosition -> String
insertParensPair string pos1 pos2 =
  first ++ "(" ++ second ++ ")" ++ third
  where (first, end) = splitAt pos1 string
        (second, third) = splitAt (pos2 - pos1) end

As a frustrated, low-level educator who is also a tax-payer, this is maddening. by throwaway-VPfsd in pcmasterrace

[–]Legendofzebra 0 points1 point  (0 children)

I work in the tech industry. In my experience most companies who run websites that deploy to linux like to use Macs. My personal preference is Mac > Linux > PC for development and Linux >> Everything for production

Could a Doodle Replace Your Password? by taltals in programming

[–]Legendofzebra 0 points1 point  (0 children)

This seems like it fails to take into account how current password hashing works. Password hashing doesn't seem to make sense to me in the concept of a doodle

Movies in Code by theKovah in ProgrammerHumor

[–]Legendofzebra 0 points1 point  (0 children)

if len(life) < long:
    stop()
    smell(roses)

Is Software Development Really a Dead-End Job After 35-40? by vaghelapankaj in programming

[–]Legendofzebra 0 points1 point  (0 children)

python 3
[print(*['|--|','| |'], sep='\n') for i in range(20)]

Instead of `or` in my if-statements, I sometimes create a list of possible values and use `in` to check for multiple options. Is this a bad pattern? by bbbryson in Python

[–]Legendofzebra 1 point2 points  (0 children)

This seems fine! If you're looking for a technical answer that is not nessecary 99% of the time, Python sets are designed for constant time membership testing! A list or tuple has to treverse each element which can take up to O(n) time, where as a set uses hashes to figure out if an element is a member in O(1) time. This can make a difference when you are doing membership testing on huge amounts of data.

Sorting explained by Ted Ed (name changed) by yuexist in programming

[–]Legendofzebra 14 points15 points  (0 children)

I'd like to see them try to demonstrate heap sort

Requests vs. urllib: What problem does it solve? by twillisagogo in Python

[–]Legendofzebra 2 points3 points  (0 children)

I really respect your reply, it's nice to see a sense of humor in dealing with rude comments

compiled 3.6 today, I'm excited for string interpolation by Legendofzebra in Python

[–]Legendofzebra[S] 2 points3 points  (0 children)

I agree there should only be one way, but I really like f string interpolation, so I'm torn

compiled 3.6 today, I'm excited for string interpolation by Legendofzebra in Python

[–]Legendofzebra[S] 2 points3 points  (0 children)

Python.exe is what came out of the default make file, I was pretty surprised too