String input = new BufferedReader(new InputStreamReader(System.in)).readLine();
Wednesday, 23 November 2016
Monday, 31 October 2016
Short Intro on Typescript
down voteaccepted | TypeScript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as you type the code.
As an example, here's some TypeScript (you can play with this in the TypeScript Playground)
And here's the JavaScript it would produce
Notice how the TypeScript defines the type of member variables and class method parameters. This is removed when translating to JavaScript, but used by the IDE and compiler to spot errors, like passing a numeric type to the constructor.
It's also capable of inferring types which aren't explicitly declared, for example, it would determine the
greet() method returns a string.
TypeScript makes typing a bit easier and a lot less explicit by the usage of type inference. For example:
var x = "hello" in TypeScript is the same as var x : string = "hello". The type is simply inferred from its use. Even it you don't explicitly type the types, they are still there to save you from doing something which otherwise would result in a run-time error.
TypeScript is optionally typed by default. For example
function divideByTwo(x) { return x / 2 } is a valid function in TypeScript which can be called with any kind of parameter, even though calling it with a string will obviously result in a runtime error. Just like you are used to in JavaScript. This works, because when no type was explicitly assigned and the type could not be inferred, like in the divideByTwo example, TypeScript will implicitly assign the type any. This means the divideByTwo function's type signature automatically becomes function divideByTwo(x : any) : any. There is a compiler flag to disallow this behavior: --noImplicitAny. Enabling this flag gives you a greater degree of safety, but also means you will have to do more typing. |
Thursday, 31 March 2016
Scala Install in Ubuntu
root@mypod:~# sudo add-apt-repository ppa:webupd8team/java root@mypod:~# sudo apt-get update root@mypod:~# sudo apt-get install oracle-java7-installer
root@mypod:~# sudo wget http://www.scala-lang.org/files/archive/scala-2.9.3.tgz root@mypod:~# sudo tar zxf scala-2.9.3.tgz root@mypod:~# sudo mv scala-2.9.3.tgz /usr/share/scala root@mypod:~# sudo ln -s /usr/share/scala/bin/scala /usr/bin/scala root@mypod:~# sudo ln -s /usr/share/scala/bin/scalac /usr/bin/scalac root@mypod:~# sudo ln -s /usr/share/scala/bin/fsc /usr/bin/fsc root@mypod:~# sudo ln -s /usr/share/scala/bin/sbaz /usr/bin/sbaz root@mypod:~# sudo ln -s /usr/share/scala/bin/sbaz-setup /usr/bin/sbaz-setup root@mypod:~# sudo ln -s /usr/share/scala/bin/scaladoc /usr/bin/scaladoc root@mypod:~# sudo ln -s /usr/share/scala/bin/scalap /usr/bin/scalap
Create a file called HelloWorld.scala using your favorite text editor. It should contain this:
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
scalac will compile this for you, and scala will run it.
root@mypod:~# scalac HelloWorld.scala root@mypod:~# scala HelloWorld
Tuesday, 15 March 2016
Increase Java and Eclipse Heap memory
In Eclipse open eclipse.ini and edit as "-vmargs -Xms512m -Xmx 1024m"
To check existing JVM memory : "java -XX:+PrintFlagsFinal -version | findstr /i"HeapSize PermSize ThreadStackSize" works only on windows
if you have 32 bit java, you can't extend your physical memory to more than 4 GB.
You can run 32 bit JVM as 64 bit y using "-d64"
Eg; java -d64 -Xms1024m -Xmx4096m yourjavapgm
To check existing JVM memory : "java -XX:+PrintFlagsFinal -version | findstr /i"HeapSize PermSize ThreadStackSize" works only on windows
if you have 32 bit java, you can't extend your physical memory to more than 4 GB.
You can run 32 bit JVM as 64 bit y using "-d64"
Eg; java -d64 -Xms1024m -Xmx4096m yourjavapgm
Wednesday, 10 February 2016
Java JPEG compression using ImageIO package
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
public class ImageIo {
public static void main(String args[])
{
try {
new ImageIo().compressImage(new File("/home/aurum/pngtest900kb.png"),"pngtest900kb.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String compressImage(File cardImage, String fileName) throws IOException {
File compressedImageFile = new File("/home/aurum/pngtest900kb.png");
InputStream is = new FileInputStream(cardImage);
OutputStream os = new FileOutputStream(compressedImageFile);
float quality = 0.3f;
if(cardImage.length() > 2048) {
quality = 0.2f;
}
BufferedImage image = ImageIO.read(is);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
writer.write(null, new IIOImage(image, null, null), param);
is.close();
os.close();
ios.close();
writer.dispose();
return compressedImageFile.getName();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
public class ImageIo {
public static void main(String args[])
{
try {
new ImageIo().compressImage(new File("/home/aurum/pngtest900kb.png"),"pngtest900kb.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String compressImage(File cardImage, String fileName) throws IOException {
File compressedImageFile = new File("/home/aurum/pngtest900kb.png");
InputStream is = new FileInputStream(cardImage);
OutputStream os = new FileOutputStream(compressedImageFile);
float quality = 0.3f;
if(cardImage.length() > 2048) {
quality = 0.2f;
}
BufferedImage image = ImageIO.read(is);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
writer.write(null, new IIOImage(image, null, null), param);
is.close();
os.close();
ios.close();
writer.dispose();
return compressedImageFile.getName();
}
}
Subscribe to:
Posts (Atom)