More Kotlin vs Java puzzlers

This commit is contained in:
Andrey Breslav
2011-11-12 18:37:06 +04:00
parent 3f6838be36
commit 89fa7cd29a
12 changed files with 154 additions and 0 deletions
+12
View File
@@ -11,6 +11,12 @@ fun array(vararg array : Float) = array
fun Any?.identityEquals(other : Any?) = this === other
fun <T : Any> T?.npe() : T {
if (this == null)
throw NullPointerException()
return this;
}
namespace io {
import java.io.*
@@ -46,4 +52,10 @@ namespace io {
}
return stdin?.readLine()
}
}
namespace string {
fun String.replaceAll(pattern : String, replacement : String) : String {
return java.util.regex.Pattern.compile(pattern).matcher(this).replaceAll(replacement).npe()
}
}
@@ -0,0 +1,12 @@
package _03_character._14_Escape_Rout;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 31). Pearson Education (USA). Kindle Edition.
*/
public class EscapeRout {
public static void main(String[ ] args) {
// \u0022 is the Unicode escape for double quote (")
System.out.println("a\u0022.length( ) + \u0022b".length( ));
}
}
@@ -0,0 +1,12 @@
namespace escape.rout
import std.io.*
import std.*
fun main(args : Array<String>) {
// \u0022 is the Unicode escape for double quote (")
println("a\u0022.length( ) + \u0022b".length)
// The actual string:
println("a\u0022.length( ) + \u0022b")
}
@@ -0,0 +1,16 @@
package _03_character._15_Hello_Whirled;
// Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 33). Pearson Education (USA). Kindle Edition.
/**
* Generated by the IBM IDL-to-Java compiler, version 1.0
* from F:\TestRoot\apps\a1\ units\include\PolicyHome.idl <-- DELETE THE SPACE BEFORE 'u'
* Wednesday, June 17, 1998 6:44:40 o'clock AM GMT+00:00
*/
public class Test {
public static void main(String[] args) {
System.out.print("Hell");
System.out.println("o world");
}
}
@@ -0,0 +1,14 @@
namespace hello.whirled
import std.io.*
import std.*
/**
* Generated by the IBM IDL-to-Java compiler, version 1.0
* from F:\TestRoot\apps\a1\units\include\PolicyHome.idl
* Wednesday, June 17, 1998 6:44:40 o'clock AM GMT+00:00
*/
fun main(args : Array<String>) {
print("Hell");
println("o world");
}
@@ -0,0 +1,13 @@
package _03_character._16_Line_Printer;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 35). Pearson Education (USA). Kindle Edition.
*/
public class LinePrinter {
public static void main(String[] args) {
// Note: \ u000A is Unicode representation of linefeed (LF) <-- DELETE THE SPACE BEFORE 'u'
char c = 0x000A;
System.out.println(c);
}
}
@@ -0,0 +1,10 @@
namespace line.printer
import std.io.*
import std.*
fun main(args : Array<String>) {
// Note: \u000A is Unicode representation of linefeed (LF)
val c : Char = 0x000A .chr;
println(c);
}
@@ -0,0 +1,18 @@
package _03_character._17_Huh;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 37). Pearson Education (USA). Kindle Edition.
*/
\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020
\u0063\u006c\u0061\u0073\u0073\u0020\u0055\u0067\u006c\u0079
\u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020
\u0020\u0020\u0020\u0020\u0073\u0074\u0061\u0074\u0069\u0063
\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028
\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0020
\u0020\u0020\u0020\u0020\u0061\u0072\u0067\u0073\u0029\u007b
\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074
\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0020
\u0022\u0048\u0065\u006c\u006c\u006f\u0020\u0077\u0022\u002b
\u0022\u006f\u0072\u006c\u0064\u0022\u0029\u003b\u007d\u007d
@@ -0,0 +1 @@
// You must be kidding
@@ -0,0 +1,16 @@
package _03_character._18_String_Cheese;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 39). Pearson Education (USA). Kindle Edition.
*/
public class StringCheese {
public static void main(String[] args) {
byte[] bytes = new byte[256];
for (int i = 0; i < 256; i++)
bytes[i] = (byte) i;
String str = new String(bytes);
for (int i = 0, n = str.length(); i < n; i++)
System.out.print((int) str.charAt(i) + " ");
}
}
@@ -0,0 +1,11 @@
package _03_character._20_Whats_My_Class;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 43). Pearson Education (USA). Kindle Edition.
*/
public class Me {
public static void main(String[] args) {
System.out.println(Me.class.getName().replaceAll(".", "/") + ".class");
}
}
@@ -0,0 +1,19 @@
namespace whats.my.`class`
import std.io.*
import std.*
import std.string.*
import typeinfo.*
class Me {
fun main() {
(this as Object).getClass()?.getCanonicalName()?.replaceAll(".", "/")
}
}
fun main(args : Array<String>) {
// Note: \u000A is Unicode representation of linefeed (LF)
val c : Char = 0x000A .chr;
println(c);
println("a\u \u0 \u00 \u000 \u0000 \u0AaA \u0AAz.length( ) + \u0022b")
}