New answers tagged

2

Anything can be an infix operator While (&) is usually the "prefix form" of an infix function, it doesn't have to be defined as infix, or even a function. The following definition of a unary function (#) with argument (&), a list of numbers, compiles and runs just fine: (#)(&)=(&)!!0+(&)!!3 This could be useful if alphabetic ...


5

Break tokens with comments A neat fact I discovered a little while ago (and the impetus for this sort of tips question in the first place), is that a block comment allows you to split tokens apart without a space. For example if you want to write main=interact tail Without using whitespace, parentheses or $ you can do the following: main=interact{--}tail ...


0

Useful escape sequences See perlrebackslash, but you can potentially save a lot of bytes using these effectively. In matches: [A-Za-z] \pl [^A-Za-z] \Pl (?<=\w) \B In replacements (or double quoted strings): \utext Text (e.g s/ (.)/uc$1/e vs. s/ (.)/\u$1/) \Utext TEXT \lTEXT tEXT \LTEXT ...


0

If you need a variable that is falsy on first call and truthy thereafter Instead of something like !!$x++, use $|++. Try it online!


0

If you need an alternating boolean Instead of doing something like $-++%2, use $|--. Try it online!


1

Numeric Literals You can now write a number literal for any user-defined type with a given/implicit instance of the FromDigits trait, such as BigInt and BigDecimal. In Scala 2, to pass the number 9,999,999,999,999,999 to a function accepting BigInts, you would have to use BigInt("9999999999999999"), but now you can just directly say ...


2

Replace simple functions with lambdas Lambdas are a generally good way to save 2 bytes(at the very least), as compared to a regular function that requires braces. Pip's lambdas, however, do not mimic the general attributes of a lambda in say, Python. You cannot shorten all standalone functions into lambdas. They're an extension of the identity function _, ...


1

M can map a value instead of a function Normally, you would use M like this: UC_M"abc" with the left-hand side being a function and the right-hand side being some iterable you want to map it to. If you need to swap the arguments, "abc"MUC_ works too. But in the case where you just want to replace every item in the iterable with a ...


1

Leave out new with creator applications You can call constructors without using new (like Kotlin), treating all classes as if they have apply methods in their companions. This saves 4 bytes every time you instantiate something.


1

Optional braces In match expressions, at least, you can save a byte by using indentation-based syntax. a match case b=>c case d=>e is 27 bytes, while the below code is 28. a match{case b=>c case d=>e}


1

Use @main and toplevel functions If you need to make a full program, use the @main annotation on a toplevel main method. @main def main={code} It's a lot shorter than these 2 approaches: object M extends App{code} object M{def main(a:Array[String])={code}}


1

Use parameter untupling Parameter untupling allows you to write functions accepting a single tuple as input as if they were functions accepting 2 parameters. Basically, you can replace these list.map{case(a,b)=>a+b} //and list.map{case(a,b)=>Seq(a,b)} //or list.map(t=>t._1+t._2) //and list.map(t=>Seq(t._1,t._2)) with this, which is both shorter ...


5

Use the Y operator From a quick survey, it looks like I use Y (or one of its variants, YP and YO) in one out of every three Pip answers--more so as the answers get more complex. Storing a value Y stands for "yank," which will be familiar to Vim users as a command that copies the current line or selection into a buffer. The unary Y operator in Pip ...


7

Use preinitialized variables Pip has many global variables that are pre-initialized so you can avoid manually writing a number/string/something else out yourself. Here are some of them (complete list): _ Identity function (== {a}) h 100 i 0 k ", " l Empty list m 1000 (mnemonic: Roman numeral M) n Newline character o 1 s Space character t ...


1

__debug__ is True Pretty self-expl... expl... >>> __debug__ True


4

Cheap divisors The usual way to get the divisors (factors) of n is: [d|d<-[1..n],mod n d<1] For example, n=6 gives [1,2,3,6]. But, if you're OK with divisors repeating out of order, you can just do: gcd n<$>[1..n] For n=6, this gives [1,2,3,2,1,6]. The code is effectively [gcd k n|k<-[1..n]], takes the greatest common divisor of k with n. ...


3

Use ~, :, and \ for variable names You can stick methods onto the back of ~. E.g. instead of this: method(i,i pop) You can do this: method(~,~pop) You can even put this between them. Instead of this: and a sqrt You can do this and~sqrt : also does this trick (I discovered it from the := bug), as well as \. (This can empirically save a lot of bytes in ...


0

Here are my 5 cents: You can plain replace Math.Max and Math.Min, the following way, to gain 6 bytes: Math.Max(a,b); // turns into: a>b?a:b; // saving 6 bytes Another hint is a small way to use a loop, when you are given a variable with "n" times that won't be used anymore (as it will flatten your variable); for (int i=0;i<n;i++) // ...


Top 50 recent answers are included