Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALL


  How to check whether a struct implements an interface in GoLang

Unlike other programming languages like Java, when implementing an interface in GoLang, there is no syntax like below to explicit tell the relationship:type I interface {}type A struct implements I {}When looking at the code or running the code in GoLang, it might lead to a case one doesn't know whether a struct implements interface before trying to use it as some interface.According to GoLang spec, as long as a struct implements all functions of an interface, it is considered as having implemented that interface and can be used wherever that interface is expected.To check whether a struct imp...

19,128 0       GOLANG INTERFACE IMPLEMENTATION CHECK


  Permutation implementation in Java

Permutation is a very basic and important mathematic concept we learned in school. It has very practical applications in real world. For example, in football.In simple, permutation describes all possible ways of doing something. For example, there are six permutations of the set {1,2,3}, namely: (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1). In general, for n items, there are n! number of permutations to arrange these items. How can we implement this algorithm in programming programming language such as Java? Below is a simple code snippet which uses the recursive method. It's quite...

7,713 4       JAVA PERMUTATION IMPLEMENTATION SAMPLE


  trim() in JavaScript

In the past, JavaScript didn't define the trim() function for String object. This makes web programmers frustrated since they often need to implement this function themselves if they want to handle user inputs. The new version of ECMA-262 introduces the trim() function.15.5.4.20   String.prototype.trim ( )     The following steps are taken:     1.   Call CheckObjectCoercible passing the this value as its argument.   2.   Let S be the result of calling ToString, giving it the this value as its argument.   3.  L...

3,734 0       JAVASCRIPT IMPLEMENTATION TRIM()


  How regular expression works

Rob Pike wrote 30 lines of codes to realize a simple regular expression matcher in his book The practice of Programming. This piece of code is really cool. Let's take a look at the code.Meaning of different characters.CharacterMeaningcGeneral character.Match any single character^Match start of a string$Match end of a string*Match zero or many occurrences of a character/*match :Test the regexp in text*/int match(char* regexp,char* text){    if(regexp[0] == '^')        return matchhere(regexp+1,text);     do{ /*Check the strin...

3,209 0       C REGULAR EXPRESSION IMPLEMENTATION ROB PIKE


  HTML5 photo taking and uploading app implementation

Underthe support of HTML5 standard, Building Web App to take photos is now possible.I will elaborate on how to take photos using Web App, display them on the pageand upload them to the server.1.     VideostreamHTML5 TheMedia Capture API provides the programmable  access to video camera on phones, users canuse the getUserMedia() method to get the video stream provided by video camera.What we need to do is to add a HTML <video> tag and make the videostream from the video camera as the input to the <video> tag.  (Please note currently only Chrome and...

24,126 1       HTML5 IMPLEMENTATION PHOTO TAKING MEDIA CAPTURE API


  Overlap Detection

How does one detect when two strings overlap? In the case below, the four-letter suffix of string 1 matches the four-letter prefix of string 2.1: "Fire at Will"2: "William Riker is number one"Sometimes there are several matches; finding the longest is not always straight forward.1: "Have some CoCo and CoCo"2: "CoCo and CoCo is here."2: "CoCo and CoCo is here."2: "CoCo and CoCo is here."The naïve solution is to take ever smaller substrings of each string, compare them, then bail out when the first match is found. This is quick ...

7,026 0       PYTHON IMPLEMENTATION STRING OVERLAP DETECTION


  Custom C++ exception class creation

In standard C++, we can use try catch to catch and exception when something goes wrong. These are some built in exception support in C++. By including the #include , we can now catch exceptions in C++ programs. This actually helps us on debugging our code and reduce the maintenance work.However sometimes if we want to create our own custom exception class. What should we do?We should include the #include line and then extend the exception class and implement some methods as you like. The general format is :#include using namespace std;class MyException:public exception{public:  &nbs...

31,471 2       C++ IMPLEMENTATION STD EXCEPTION CUSTOM EXCEPTION


  A Javascript When Function

function when(conditionFunc, execFunc, interval){ if (conditionFunc()){ execFunc(); }else{ setTimeout(function(){ when(conditionFunc, execFunc, interval);}, interval); }}You have some code which should only execute when a condition is true. E.g. You have code which relies on a javascript library but can't be sure the library has loaded yet and don't want the code to execute until it has. This is common if you have a bookmarklet which injects more than one <script> tag or you have code which should only execute when an image or other asynchronously loaded element is...

3,677 0       JAVASCRIPT IMPLEMENTATION WHEN WHEN FUNCTION