유닉스 시간은 유닉스 Epoch(1970-01-01 00:00:00 +0000 (UTC))로 부터 지난 초 단위 시간을 말합니다. C time_t now = time(NULL); #include <time.h> C++ time_t now = time(nullptr); #include <ctime> CoffeeScript now = Date.now() / 1000 now = new Date().getTime() / 1000 Java long now = System.currentTimeMillis() / 1000; long now = new Date().getTime() / 1000; import java.util.Date; JavaScript now = Date.now() / 1000; now = new Date().getTime() / 1000; Kotlin val now = System.currentTimeMillis() / 1000 val now = Date().getTime()/1000 import java.util.Date Lua now = os.time() Objective-C NSTimeInterval now = [[NSDate date] timeIntervalSince1970]; Perl $now = time; PHP $now = time(); Python time.time() import time
C 해당 기능이 존재하지 않음 C++ double logarithm(double x, double base=10) { return log(x) / log(base); } CoffeeScript logarithm = (x, base=10) -> Math.log(x) / Math.log(base) Java double logarithm(double x) { return logarithm(x, 10); } double logarithm(double x, double base) { return Math.log(x) / Math.log(base); } use method overloading JavaScript function logarithm(x, base) { if (base==null) { base = 10; } return Math.log(x) / Math.log(base); } Kotlin fun logarithm(x: Double, base: Double=10.0): Double { return Math.log(x) / Math.log(base) } Lua function logarithm(x, base) base = base or 10 return math.log(x) / math.log(base) end} Objective-C 해당 기능이 존재하지 않음 Perl sub logarithm { my $x = shift; my $base = shift // 10; log($x) / log($base); } PHP function logarithm($x, $base=10) { return log($x) / log($base); } Python def logarithm(x, base=10): return math.log(x) / math.log(base)
C const char *str = "2016-12-05"; regex_t rx; if (regcomp(&rx, "([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})", REG_EXTENDED)==0) { regmatch_t m[4]; if (regexec(&rx, str, 4, m, 0)==0) { int year = str2int(str+m[1].rm_so, m[1].rm_eo - m[1].rm_so); int month = str2int(str+m[2].rm_so, m[2].rm_eo - m[2].rm_so); int day = str2int(str+m[3].rm_so, m[3].rm_eo - m[3].rm_so); } regfree(&rx); } #include <regex.h> int str2int(const char *str, int len) { int value = 0; char *tmp = strndup(str, len); value = strtol(tmp, (char **)NULL, 10); free(tmp); return value; } C++ string str("2016-12-05"); regex rx("(\\d{4})-(\\d{2})-(\\d{2})"); smatch m; if (regex_match(str, m, rx)) { int year = stoi(m.str(1)); int month = stoi(m.str(2)); int day = stoi(m.str(3)); } #include <regex> CoffeeScript str = '2016-12-05' if /(\d{4})-(\d{2})-(\d{2})/.test str year = Number RegExp.$1 month = Number RegExp.$2 day = Number RegExp.$3 Java String str = "2016-12-05"; Pattern rx = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})"); Matcher m = rx.matcher(str); if (m.matches()) { int year = Integer.parseInt(m.group(1)); int month = Integer.parseInt(m.group(2)); int day = Integer.parseInt(m.group(3)); } import java.util.regex.Matcher; import java.util.regex.Pattern;
C // basic tmp = x; x = y; y = tmp; // function void swap(int *a, int *b) { int tmp; tmp = *b; *b = *a; *a = tmp; } swap(&x, &y); // no temporary variable x = x + y; y = x - y; x = x - y; // no temporary variable 2 x = x ^ y; y = x ^ y; x = x ^ y; C++ swap(x, y); #include <algorithm> CoffeeScript [x, y] = [y, x] Java tmp = x; x = y; y = tmp; JavaScript // basic tmp = x; x = y; y = tmp; // tricky one-line x = [y, y = x][0]; // ES2015 (ES6) [x, y] = [y, x] Kotlin tmp = x x = y y = tmp Lua x, y = y, x Objective-C tmp = x; x = y; y = tmp; Perl ($x, $y) = ($y, $x); PHP // basic list($x, $y) = array($y, $x); // function function swap(&$x, &$y) { $tmp = $x; $x = $y; $y = $tmp; } Python x, y = y, x Ruby x, y = y, x Swift swap(&x, &y)
C const char *const strs[] = {"one", "two", "three"}; int i = 0; int size = 0; char *out; for (i=0;i<3;i++) { size += strlen(strs[i]) + 1; } out = (char *)malloc(size); strcpy(out, strs[0]); for (i=1;i<3;i++) { strcat(out, ","); strcat(out, strs[i]); } C++ // one-liner vector<string> strs = {"one", "two", "three"}; string out = accumulate(next(strs.begin()), strs.end(), strs[0], [](const string &a, const string &b) { return a + "," + b; }); // prevent reallocation vector<string> strs = {"one", "two", "three"}; ostringstream os; copy(strs.begin(), strs.end()-1, ostream_iterator<string>(os, ",")); os << *strs.rbegin(); string out = os.str(); #include <algorithm> // copy #include <iterator> // next, ostream_iterator #include <numeric> // accumulate #include <sstream> // ostringstream CoffeeScript strs = ['one','two','three'] out = strs.join ',' Java // Java 8 List<String> strs = Arrays.asList("one", "two", "three"); String out = String.join(",", strs); // Java 7 and below String[] strs = {"one", "two", "three"}; StringBuilder sb = new StringBuilder(); boolean first = true; for (String str : strs) { if (first) { first = false; } else { sb.append(","); } sb.append(str); } String out = sb.toString(); JavaScript let strs = ['one','two','three'] let out = strs.join(',') Kotlin val strs = arrayOf("one", "two", "three") val out = strs.joinToString(",")
C const char *s1 = "hello"; const char *s2 = " world"; char *s3 = (char *)malloc(strlen(s1)+strlen(s2)+1); strcpy(s3, s1); strcat(s3, s2); C++ // destructive string s1 = "hello"; s1 += " world"; // non-destructive string s1 = "hello"; string s2 = s1 + " world"; CoffeeScript 'hello' + ' world' Java "hello" + " world" JavaScript 'hello' + ' world' Kotlin "hello" + " world" Lua 'hello' .. ' world' Objective-C // destructive NSMutableString *s1 = [@"hello" mutableCopy]; [s1 appendString:@" world"]; // non-destructive NSString *s1 = @"hello"; NSString *s2 = [s1 stringByAppendingString:@" world"]; Perl 'hello' . ' world' PHP 'hello' . ' world' Python 'hello' + ' world' Ruby 'hello' + ' world' Swift "hello" + " world"