# Tips for golfing in Japt

Now that I'm thoroughly addicted to Code Golf, it's probably about time that I try to pick up a few golfing languages.

Given that I play almost exclusively in JavaScript, Japt seems like the logical language to start with. I'll be diving into the documentation at the next opportunity I get but, in the meantime, please post any tips you have for Japt in the answers below.

As I'm a beginner in Japt and golfing languages in general, if you could "translate" your tips to JavaScript, where possible, that would be a big help in helping me get to grips with things.

• Heh, thanks for posting this. I'd been holding off on doing this because I'd like to redesign Japt at some point, but that won't be happening anytime soon, and it probably won't mess up a lot of the tips anyway. Tip for myself: write a tutorial :P – ETHproductions May 17 '17 at 21:31
• Don't forget to visit the Japt chatroom :) – Oliver May 18 '17 at 14:53

## Moving from JavaScript to Japt

As you may know, Japt is simply a shortened, extended version of JavaScript. I created Japt because I was tired of long property names, like String.fromCharCode(x) and Math.floor(x), and the tediousness of doing things such as creating a range. Here's the bare minimum you need to know when going from JavaScript to Japt:

• Japt is a transpiled language; Japt code is transpiled to JavaScript and then run as JS. (I guess you could say compiled, but transpiled sounds more hipster. Disclaimer: I know absolutely nothing about being hipster)
• All entries are full programs by default. The input is implicitly parsed, and the first six inputs are put into the variables U, V, W, X, Y, and Z; the full array is stored in N. The result of the last expression is automatically printed.
• All uppercase letters are variables, and stay the same when transpiled. Most have preset values, which you can find in the "Variables" section of the Japt docs (at the interpreter).
• All lowercase letters are prototype functions, or methods. Japt adds the methods a-z (and à-ÿ) on numbers, strings, and arrays. When you use one of these letters, Japt fills in the . and (; Uc in Japt is equivalent to U.c( in JavaScript, which could mean ceil, charCodeAt, or concat, depending on the type of U. This is where most of Japt's power comes from; you can find full lists of these methods under the "_____ functions" sections of the Japt docs (at the interpreter).
• A space represents ), and ) represents )). This is because when I first designed Japt, I wanted to save as many bytes as possible, and that's how I first thought of doing this. (Though Us w n does look better than Us)w)n), IMHO.)
• A function is denoted as ABC{...}, where the ABC can be any string of variables. Functions work for the most part as they do in JS, the main difference being the last expression is automatically returned (rather than having to use return or fancy ES6 parentheses).
• ' denotes a single char string (i.e. 'a is the same as "a"), and # takes the next char-code and becomes that number (#e is the same as 101).

$alert("hello world")$ can be shortened to $alert("hello world" ### Compressing JavaScript You can also compress JavaScript using Ox. If there is a JavaScript function that you want to use, say screen.width, you can compress the string "screen.width" using Oc, then inserting the result in between Ox... Note that you do not need closing quotes in Japt when it is not followed by anything else. • @Shaggy You need the Ox to evaluate the string. Otherwise, you would just output the text "screen.width". Example – Oliver May 18 '17 at 13:06 # Know the flags As per the latest meta consensus (Dec 2017), command-line flags are no longer counted towards bytes. It's really a great news for Japt since it has many flags for extra treatment on input/output. All available flags in Japt are described below, in the order of evaluation. The flags in the same group are exclusive to each other. Note that the flags in different groups can be used in combination, resulting in something like this :) ## mdefæ The whole program is mapped over the first argument (U). If more arguments are present, they are passed as-is (i.e. not pairwise mapped). Otherwise, the second argument is the index, and the third is the whole array, just like U.m. If U is a number, it is converted to range; if string, it's converted to array of chars and the results are joined together. • -m: Applies the above and nothing else. • -d: Returns true if some result is truthy, false otherwise. • -e: Returns true if all results are truthy, false otherwise. • -f: Returns the array of elements of U whose results are truthy. • -æ: Applies -f and returns its first element. ## gh Takes an element at the specified index. • -g: Takes first element (index 0). • -gX: Takes the element at index X (can be any positive integer). • -h: Takes last element. ## !¡ Convert the result to boolean. • -!: Apply boolean not. • -¡: Apply boolean not twice (returns the truthiness). ## N Convert the result to number. The unary plus is used. ## PRSQ Convert to string of some sort. • -P: Join the array with "". • -R: Join the array with "\n". • -S: Join the array with " ". • -Q: Apply JSON.stringify (can be any object, not only an array). Example. ## x Applies the function x to the output. (It's literally x, not "any single lowercase alphabet function".) • Array: Sum. • String: Trim from both ends. • Number: Round to integer. • Note that using flags doesn't count as a Japt submission though, it counts as a Japt-with-those-specific-flags-language submission. – Etheryte Jul 17 '18 at 8:04 ## Take advantage of preset variables Variables A-S are preset to common values that take more than one byte to represent in Japt: • A-G are 10-16. • H is 32, I is 64, J is -1, L is 100. • K is defined as new Date(), which you can manipulate in various ways. • M and O are objects with various useful functions. You can learn more in the docs. • P is the empty string, Q is a quote mark, R is a newlines, and S is a space. • T is set to 0, so you can use it as an accumulator if necessary. If the first character in the program is a semicolon ;, A-L are reset as follows: • A is the empty array []. • B is "ABCDEFGHIJKLMNOPQRSTUVWXYZ". • C is "abcdefghijklmnopqrstuvwxyz". • D is "QWERTYUIOP\nASDFGHJKL\nZXCVBNM". • E is "[a-z]", and F is "[A-Za-z]" (useful before I added these as regex features) • G is 36, H is 65, and I is 91 (useful for alphabet ranges). • J is a single comma; L, a single period. Nowadays only A, B, C, and D from this list are really useful. I'm planning to add a better system which allows up to 256 two-byte variables, which will be preset to these values and a whole lot more. ## Unicode shortcuts There are many common structures in Japt that just can't be stored in a single ASCII char, such as qS , p2 , mX{, } , etc. So to get around this, Japt has "Unicode shortcuts", which are characters in the range \xA1-\xDE (¡-Þ) which expand to these common structures. You can find a full list of these in the interpreter docs. Additionally, @ stands for XYZ{, and _ stands for Z{Z, to help build functions. So let's golf our example program from another answer: UmX{Xc0 s16}  Firstly, we can replace X{X with _, which gives us: Um_c0 s16}  Then we can replace m_ with ® saving another byte: U®c0 s16}  Or we could replace X{ with @, which gives us: Um@Xc0 s16}  This then allows us to use the ¡ shortcut to save two bytes: ¡Xc0 s16}  One of these two paths can be shortened 1 byte more than the other. Can you figure out which? • ®c s16 for 6 bytes - do I win a cookie?! – Shaggy May 18 '17 at 14:02 • @Shaggy You can save 1 more byte if you look hard enough... ;) – ETHproductions May 18 '17 at 14:19 • Would it be ®c sG? – Shaggy May 18 '17 at 14:20 • Yep! I think that's as low as you can go. Well done! :-) – ETHproductions May 18 '17 at 14:23 • Amazing looking back on these, seeing the progression of Japt in a few short months. This can now be achieved with csG. – Shaggy Sep 13 '17 at 22:10 # Use auto-functions You most likely already know that @ and _ are shortcuts for XYZ{ and Z{Z, respectively (covered in the Unicode shortcuts answer). But sometimes you can make functions even shorter. Suppose you had an array of characters and you wanted to map each character to its char-code. You could do this with either of these: mX{Xc} m_c}  But there's a better way. If a method or operator is the first item after another method or a (, it gets turned into a string. So these two lines are equivalent: r'a'b // Replace all "a"s with "b"s; transpiles to .r("a","b") ra'b // Does the same thing, 1 byte less; transpiles to the same thing  But how does that help with our functions? Well, most methods that accept functions, if given a string representing a method or operator, will interpret it as a function. Which means you can also do this: m_c} // Map each item to its char code m'c // Does the same thing, 1 byte less mc // Also does the same thing, 2 bytes less  I call these "auto-functions". There are several different varieties: • m@Xc}mc • m@Xc1}mc1 • m@X+1}m+1 • m@1+X}m!+1 • m@2pX}m!p2 Hopefully you get the idea. To swap the arguments, just prefix the method or operator with !. • Would it be worth noting here that using auto-functions can also allow for further savings through the use of shortcuts? e.g., m@2pXÃm!p2<space>m!². – Shaggy Sep 13 '17 at 21:51 • Woah! I didn't thought about using a string in map, didn't even know it's possible. Maybe I'll save few bytes thanks to this in the future. – RedClover Jan 12 '18 at 14:38 • Hey @Soaku, I somehow missed that you've been answering with Japt, so allow me to extend you a late Welcome! Hope you've enjoyed using it so far. If you have any questions, suggestions, or just want to talk, feel free to join us in the Japt chatroom (Github usually works as well for the first two ;) ) – ETHproductions Jan 13 '18 at 0:22 # Implicit Variable Assignment Whenever you start a new line in Japt the result of the previous line is automatically assigned to one of the input variables (U-Z), with the first line being U, the second V, and so on. Let's take an example: say you wanted to create 2 arrays to work with, one containing the numbers 1-10 and the other containing their squares. The long way to do this would be like so: U=Aõ V=Um² [do something with the arrays]  Using automatic variable assignment, though, that can be shortened to: Aõ Um² [do something with the arrays]  We've saved 4 bytes there. But, in this instance, we can save one more byte because the array of 1-10 is assigned to U and U can be omitted in certain scenarios: Aõ m² [do something with the arrays]  ## Caution One thing to be careful of with this tip is that you don't overwrite any input variables you might need later on in your programme. This can be avoided by leaving one or more empty lines at the start of it. In the following example, the 2 arrays will be assigned to the variables V & W, instead of U & V:  Aõ Vm² [do something with the arrays] # Know the Javascript Since any Japt code runs as transpiled JS, good understanding of JS operators and built-in methods helps a lot in golfing pieces of Japt code. ### Relevant JS tips []Vm@... ...  • Short-circuiting • Splitting with numbers • This can be generalized to any method that accepts strings but not numbers. A number passed there will implicitly cast to a string, often saving a byte (e.g. 0 over '0). ### Relevant JS built-in functions Closely look at what parameters are passed to function arguments. For string methods, it's good to know how the behaviors differ between passing a string or regex with or without g flag. # Use multiple lines when necessary For most not-too-hard challenges, you can express the solution in just one line of Japt, as a sequence of applying built-in functions. But more complex ones will require to use looping constructs, recursion, or reusing large chunks of code. This is where multi-line programming comes in. ## Remove closing parens Task: Given an array of numbers, pair each element with the index squared, and sort it by the sum. [5,1,17,9,3] => [[5,0],[1,1],[17,4],[9,9],[3,16]] => [[1,1],[5,0],[9,9],[3,16],[17,4]] One-line solution is íUm@Yp2})ñx, but }) costs two bytes (and there is no one-byte shortcut). You can remove }) by simply moving the trailing ñx to the next line, so the code looks like this: íUm@Yp2 ñx  and the transpiled JS becomes: U = U.í(U.m(function(X, Y, Z) { return Y.p(2) })); U.ñ("x")  You can clearly see this does the same thing as the one-line solution, just assigning the intermediate result back to U. ## Recurse with implicit arguments The recursion function ß takes all of UVWXYZ as implicit parameter, if not specified. U is obviously the main input, but you can use any of VWXYZ to keep track of other values you need. For example, you can do something like the following: (modify input and implicit assign to U) (modify V and implicit assign to V) (test something and call ß without arguments; U and V are passed automatically)  Alternatively, if all you want is a temporary variable, you can use inline assignment, like (T=...), as the variable T (0) is rarely used as-is. ## Reuse a long function For this, I don't think I can come up with a good example task, so I'll reference the only solution this tip was used, and just outline some general ideas. • In order to reuse a function, you need to store it in a variable. Starting a line with the function-opener {, @ or _ does the job. Alternatively, you can also do something like (T=@...}) to embed the function assignment inside a more complex line. • It's actually not trivial to call the stored function. Suppose V is a function and we want to call V(U) in JS. VU does not work since it simply means V,U. V(U doesn't either; it's V,(U). Even the function methods aren't helpful very much. The best way we found is: • [U]xV (map and sum) if the result is number • UmV if U is a single char and V returns a string, or • $V($U or [U]mV g in general. • However, mapping or looping with it is rather easy. To map over an array, use UmV. To find the first integer that satisfies V, use Va. # Fun with Auto-Functions As a follow-up to ETH's general tip on auto-functions, this tip will provide a few specific examples of byte saving tricks you can achieve with them, which I'll add to as I think of more. ## Get the largest integer in an array. Assume we have the array [3,1,4,2] assigned to variable U and we ant to retrieve the largest number from it. We could do it in 4 bytes by sorting the array and then popping the last element: Un o  The downside to that is that we've modified the original array; U is now [1,2,3] which may not always be desirable. Luckily, there's a way of doing it without modifying the array that's also one byte shorter: Urw  What we've done there is reduced the array using the w method, which, when used on an integer returns the larger of the integer and the method's argument (e.g., 2w5 returns 5). So the above is the equivalent of UrÈwY or UrXY{XwY}. Note, though, that this tip won't work in the case of all the integers in the array being negative. • Side note: I'm planning to add functions to get the min and max of an array, though probably only in v2. – ETHproductions Jul 10 '17 at 17:23 ## When not to use í í is a useful built-in that pairs (or zips) two arrays or strings, and optionally maps each pair through a function. However, it currently has a few minor issues when given uneven arrays or strings: • If the first array has more items than the second, the non-existent items in the second will be given as undefined. • If the second array has more items than the first, it will stop processing at the end of the first array. This can make it difficult to, say, compare two uneven strings and take the char with the higher code point from each pair. Even if you know that U is going to be the longer one, it still takes far to many bytes to solve this simple task: UíUç hV @[XY]n o  What you could do instead would be to take input as an array of two strings, transpose the array with y, and then map each row to the correct result: Uy m_q n o  This has the advantage of always padding the shorter string with spaces, making it a piece of cake to go through the entirety of both strings. Real-life examples: 1, 2 # Generate the ASCII Range Update: Japt now has a constant for the ASCII range; the alternative value for E, accessible with ;. See this tip for more on Japt's constants. While Japt doesn't (yet) have a built-in for the ASCII range, you can generate an array of characters in just 5 bytes: 95odH  Try it ## How It Works 95o creates the range [0,95) with each element being passed through the auto-function d which, when used on a number, returns the character at that codepoint. Pass a number as an argument to the d method, in this case H, the Japt constant for 32, and it will be added to the original number before being converted. An equivalent solution in JavaScript would be: [...Array(95)].map((_,x)=>String.fromCharCode(x+32))  ## Random Characters To get a random character in the ASCII range, use ö instead, which returns a random number from the range [0,X), where X is the number it is run on. 95ö dH  Or, to get an array of multiple random characters, pass the number of characters you need as an argument of ö. The following will return 10 characters: 95öA mdH  ## Remove unnecessary structural chars By structural chars, I mean {}, (), $, even " and . You can typically remove these chars whenever they occur right at the end of a program (e.g. UmX{Xc +"; "} -> UmX{Xc +"; ).

Additionally, you can remove parens or spaces whenever they appear in the following places:

• Up against a semicolon ; (or the end of the program);
• To the right of { (and by extension, @) or [, or the left of ] or }.

Also, commas are very rarely needed to separate arguments. If you write AB, for example, Japt knows you mean A and B separately. You only really need a comma to separate two numerical literals, such as Us2,5.

Finally, if there's a U at the start of a program or after a { or ;, followed by a method call (lowercase letter or related Unicode shortcut) or any binary operator excluding + and - (*, &, ==, etc.), you can remove the U to save a byte and Japt will insert it for you.

• I've found a few other instances where U can be ommited even when it's not at the start of the programme. – Shaggy May 18 '17 at 19:55
• @Shaggy Oh right, it also works after a { or ;. Are there any others that you are aware of? (It's been a while since I coded this feature :P) – ETHproductions May 18 '17 at 19:59
• Can't think of them off the top of my head; I'll check it out again when I get back to my computer tomorrow. – Shaggy May 18 '17 at 20:03

# Modify the Last Element in An Array

Sometimes you may need to modify the last element in an array so here's an explanation of a short way of doing that. We'll be working with the array [2,4,8,32] assigned to input variable U and dividing the last integer (32) by 2.

The obvious way to achieve this would be with this 9 byte solution (Demo):

UhJUgJ /2

• hnx sets the element at index n to x.
• gn returns the the element at index n.
• J is the Japt constant for -1, which, thanks to Japt's support for negative index, allows us to work with the last element in an array; handy when you don't know the size of the array.
• And /2 is simply division by 2.

So the above sets the element at index -1 in the array to the element at index -1 in the array divided by 2. Or in JavaScript: U[3]=U[3]/2. When you write it out like that, it seems a far too long-winded way of going about it. Luckily, there is a shorter way; we could pop the last element from the array, modify it and push it back to the array. Performing each of those operations individually would take more than 9 bytes but we can do them all at once for just 7 bytes, a 2 bytes saving (Demo)

UpUo /2


Translated to JS, it's the equivalent of:

U.push(U.pop()/2)&&U