-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
134 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package utf8 | ||
|
||
/** multibyte UTF8 playground */ | ||
object Playground extends App { | ||
|
||
def toBin(x: Byte): String = { | ||
val chars = Array.ofDim[Byte](8) | ||
(0 to 7) | ||
.foreach { bit => | ||
chars(7 - bit) = ('0' + ((x >> bit) & 1)).toByte | ||
} | ||
new String(chars) | ||
} | ||
|
||
def mkColored(s: String, n: Int): String = | ||
new StringBuilder(Console.RED) | ||
.append(s.substring(0, n)) | ||
.append(Console.RESET) | ||
.append(s.substring(n)) | ||
.toString() | ||
|
||
def describeUtfContent(utf: String): Unit = { | ||
val bytes: Array[Byte] = utf.getBytes | ||
val per_char = bytes.length / utf.codePoints().count().toInt | ||
val dec: Array[Int] = bytes.map(b => b & 0xff) | ||
val hex: Array[String] = bytes.map(b => "%02X".format(b)) | ||
val bin: Array[String] = bytes.zipWithIndex.map { case (b, i) => | ||
mkColored( | ||
toBin(b), | ||
(per_char, i % per_char) match { | ||
case (1, _) => 1 | ||
case (x, 0) => x + 1 | ||
case (x, _) if x < 8 => 2 | ||
case _ => 0 | ||
} | ||
) | ||
} | ||
|
||
def groupGt1[A](xs: Iterable[A]) = | ||
per_char match { | ||
case 1 => xs.mkString("[", ", ", "]") | ||
case _ => xs.grouped(per_char).map(_.mkString("[", ", ", "]")).mkString("[", ", ", "]") | ||
} | ||
|
||
printf("content: `%s`\n", utf) | ||
printf("length: %d\n", utf.chars().count()) | ||
printf("bytes length: %d\n", bytes.length) | ||
printf("bytes per char:%d\n", per_char) | ||
printf("bytes decimal: %s\n", groupGt1(dec)) | ||
printf("bytes hex: %s\n", groupGt1(hex)) | ||
printf("bytes bin: %s\n", groupGt1(bin)) | ||
println("-" * 50) | ||
} | ||
|
||
Seq( | ||
"hello", | ||
"Привет", | ||
"नमस्ते", | ||
"😀🤪😐🙄" | ||
).foreach(describeUtfContent) | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package interview.general | ||
|
||
object Q3_Syntax extends App { | ||
|
||
implicit class IntWithOptionSyntax(x: Int) { | ||
def +(yo: Option[Int]): Int = | ||
yo match { | ||
case Some(y) => x + y | ||
case None => x | ||
} | ||
} | ||
|
||
val x: Int = 10 + Some(33) | ||
println(x) | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package interview.general | ||
|
||
object TTT { | ||
|
||
trait A { | ||
def a1: Int | ||
def a2: String | ||
} | ||
|
||
new A { | ||
override val a1: Int = 1 | ||
override val a2: String = "hello" | ||
} | ||
|
||
class C(val a1: Int, val a2: String) extends A { | ||
def m1: Int = ??? | ||
} | ||
object C { | ||
def apply(a1: Int, a2: String) = new C(a1, a2) | ||
} | ||
|
||
|
||
new C(1, "hello") | ||
C.apply(1, "hello") | ||
C(1, "hello") | ||
|
||
val f = new Function[Int, String] { | ||
override def apply(v1: Int): String = v1.toString | ||
} | ||
|
||
f.apply(3) | ||
f(3) | ||
|
||
val a: Array[Int] = Array(1,2,3,4,5) | ||
a.apply(2) | ||
a(2) | ||
|
||
"hello".apply(3) | ||
"hello"(3) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.9.5 | ||
sbt.version=1.9.6 |