Last active
February 23, 2018 02:54
-
-
Save Ivanca/6c8b2816a13312dd7f95509bd5c335a3 to your computer and use it in GitHub Desktop.
Q adverts translated to python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Each-prior | |
##################################################################### | |
# in Q >>> | |
types: type'[(1;2h;3.2)] | |
# returns the type of each element (-7 -5 -9h) | |
# in PYTHON >>> | |
numbers = [1, 2, 3.2] | |
types = list(map(type, numbers)) | |
# Notes: | |
# 1) executing type'[(1;2h;3.2)] is the same as type each [(1;2h;3.2)] | |
# 2) 2h (unlike just 2) is a 'short', a number between 0 to 255 (there is no python equivalent for that) | |
# Over | |
##################################################################### | |
# In Q >>> | |
result: {x*2}/[4;3] | |
# | |
# In Q Destructured >>> | |
repeat: 4; | |
initialValue: 3; | |
result: {x*2}/[repeat;initialValue] | |
# | |
# Which is the same as executing 3*2*2*2*2 | |
# | |
# in PYTHON >>> | |
def timesTwo(x): | |
return x * 2 | |
repeat = 4 | |
initialValue = 2 | |
result = initialValue | |
for x in range(repeat): | |
result += timesTwo(result) | |
print(result) # result is now 58 | |
# Notes: | |
# There is also a monadic way of doing this in python use 'reduce', more | |
# similar to the way Q does it | |
# you may read about it at https://www.geeksforgeeks.org/reduce-in-python/ | |
# In Q when the second one being a list, the function is called with the left argument as its | |
# first parameter and the first element of the right argument as the second parameter; meaning: | |
ssr["today I will";"today";"tomorrow"] | |
# returns "tomorrow I will" but... | |
ssr/["today I will";("today";"I");("tomorrow";"you")] | |
# returns "tomorrow you will" | |
# Scan | |
##################################################################### | |
# The same as "over" but returns an array filled with the result of each iteration | |
# There is also a monadic way of doing this in python use 'reduce' | |
def timesTwo(x): | |
return x * 2 | |
repeat = 4 | |
initialValue = 2 | |
result = [initialValue] | |
for x in range(repeat): | |
result += [timesTwo(result[-1])] | |
# Dyadic | |
##################################################################### | |
# In Q >>> | |
1 +' 1 2 3 | |
# this returns 2 3 4 | |
# In PYTHON >>> | |
list(map(lambda x : 1 + x, [1, 2, 3])) | |
# In Q >>> | |
5 -' 1 2 3 | |
# this returns 4 3 2 | |
# In PYTHON >>> | |
list(map(lambda x : 5 - x, [1, 2, 3])) | |
# Notes: | |
# In Q this operator doesn't care for the order so 1 +' 1 2 3 is the same as 1 2 3 +' 1 | |
# If both parameters are lists then they must be of the same length. | |
# Each prior | |
##################################################################### | |
# Takes pairs of elements and executes an operator | |
# example using sum "+" operator | |
# In Q >>> | |
result: +':[4 8 3 1 2] | |
# returns 4 12 11 5 4 (cause 0+4 is 4, 4+8 is 12, and so on) | |
# In PYTHON >>> | |
lista = [4,8,3,1,2] | |
result = [] | |
# First element gets no changes, its just copied | |
result.append(lista[0]) | |
for index in range(1,len()): | |
result.append(lista[index] + lista[index - 1]) | |
# Each-Right | |
##################################################################### | |
# In Q >>> | |
"abc",/:("cde";"fgh";"ijk") | |
# In PYTHON >>> | |
result = list(map(lambda x: "abc" + x, ["cde","fgh","ijk"])) | |
print(result) | |
# Each-Left | |
##################################################################### | |
# The each-left adverb behaves the same way as the each-right adverb, except it applies the right | |
# argument to each element of the left argument. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment